473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why can't I have a vector<string[]>?

Hello,

I am new to C++. I know the reason is probably template instantiation
problems ... but what's the *real* reason I cannot declare a:

vector<string[]v = vector<string[]>(4);
Thanks!
--Shafik

Apr 28 '07 #1
10 2460
Shafik wrote:
Hello,

I am new to C++. I know the reason is probably template instantiation
problems ... but what's the *real* reason I cannot declare a:

vector<string[]v = vector<string[]>(4);
The type of a vector (i.e. T in vector<T>) needs to support copy. The
standard does not allow copy of arrays.

i.e.

int a[2];
int b[2];

b=a; // not allowed.
The other problem you have is that string[] is not really a fully
defined type (i.e. no size).

Try this:
vector< vector<string v = vector< vector<string[](4);
Apr 28 '07 #2
On Apr 28, 4:44 pm, Shafik <shafi...@gmail.comwrote:
Hello,

I am new to C++. I know the reason is probably template instantiation
problems ... but what's the *real* reason I cannot declare a:

vector<string[]v = vector<string[]>(4);

Thanks!
--Shafik
string[] means nothing at all, there is no such thing as an array with
no fixed constant size.
A std::vector requires copy-constructeable and assigneable elements
too (thats the law).

Besides, why would you ever want a vector< string[const size_t] >?
since:
std::vector< std::vector< std::string vvs;

Apr 28 '07 #3
Do you want vector that store array of string, right?
Maybe this may work of you

vector<string*var;

About string[], I guess ... usually we use [] after variable to make
it an array but string is techniquely not variable;it's a class.

Apr 28 '07 #4
Shafik wrote:
Hello,

I am new to C++. I know the reason is probably template instantiation
problems ... but what's the *real* reason I cannot declare a:

vector<string[]v = vector<string[]>(4);
If you already know that you could use a vector instead of an array, why are
you trying to make a vector of arrays? Just make a vector of vectors:

vector<vector<string v(4);

Apr 28 '07 #5
On Apr 28, 6:32 pm, Kouisawang <KOuisaw...@gmail.comwrote:
Do you want vector that store array of string, right?
Maybe this may work of you

vector<string*var;

About string[], I guess ... usually we use [] after variable to make
it an array but string is techniquely not variable;it's a class.
Don't confuse
string* and string[const int]
....and the same goes with...
char* and char[const int]
....they have nothing in common other than the fact that a pointer can
point to a specific ellement of an array.

std::vector< std::string* vps;

is a collection of pointers to std::string, not a collection of
arrays.
If your suggestion is something like:

#include <iostream>
#include <vector>

int main()
{
const int Size( 4 );
std::string array[ Size ];
for(size_t u = 0; u < Size; ++u)
{
array[ u ] = "a short string";
}

std::vector< std::string* vps;
vps.push_back( &array[ 0 ] );

for ( size_t i = 0; i < vps.size(); ++i )
{
std::string* p_s = vps[ i ];
for ( size_t u = 0; u < Size; ++u )
{
std::cout << *p_s++ << std::endl;
}
}
}

/*
a short string
a short string
a short string
a short string
*/

Then thats a pretty ridiculous solution since the equivalent using
std::vector< std::vector< T is much simpler, infinitely more
powerfull and a breeze to maintain. Notice that a single statement is
required to initialize all elements. The same templatized operator<<
is generated once for the vector of strings and again for the vector
of vectors.

#include <iostream>
#include <ostream>
#include <vector>

template< typename T >
std::ostream&
operator<<( std::ostream& os,
const std::vector< T >& r_vt )
{
typedef typename std::vector< T >::const_iterator TIter;
for(TIter iter = r_vt.begin(); iter != r_vt.end(); ++iter)
{
os << *iter << std::endl;
}
return os;
}

int main()
{
typedef std::vector< std::string VStrings;
// 4 x 4 matrix of strings
std::vector< VStrings vvs(4, VStrings(4,"a short string"));

std::cout << vvs;
}

/*
a short string
a short string
a short string
a short string

a short string
a short string
a short string
a short string

a short string
a short string
a short string
a short string

a short string
a short string
a short string
a short string
*/

The above code stays the same whether your vector stores 4 or a
thousand vectors of strings.
It will also work with any type with an op<< overload (ie: all
primitives).

std::vector< std::vector< double vvd(1000, std::vector< double
>(10, 1.1));
std::vector< std::vector< int vvd(10, std::vector< int >(100, 0));
Apr 29 '07 #6
Kouisawang wrote:
Do you want vector that store array of string, right?
Maybe this may work of you

vector<string*var;

About string[], I guess ... usually we use [] after variable to make
it an array but string is techniquely not variable;it's a class.
It's also not an array. Empty [] have very limited meaning. If you
are using them, you are almost always doing the wrong thing.
Apr 29 '07 #7
On Apr 29, 12:32 am, Kouisawang <KOuisaw...@gmail.comwrote:
Do you want vector that store array of string, right?
Maybe this may work of you
vector<string*var;
About string[], I guess ... usually we use [] after variable to make
it an array but string is techniquely not variable;it's a class.
The token sequence std::string[] is a perfectly valid type
specification in C++, although I can't imagine a context in
which I would use it in practice. Gianni correctly explained
why you cannot use it to instantiate an std::vector: it doesn't
support copy and assignment. (In this case, there's also the
fact that the type isn't complete, and you can't instantiate
anything in the standard library over an incomplete type.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 29 '07 #8
On Apr 29, 2:10 pm, Ron Natalie <r...@spamcop.netwrote:
Kouisawang wrote:
Do you want vector that store array of string, right?
Maybe this may work of you
vector<string*var;
About string[], I guess ... usually we use [] after variable to make
it an array but string is techniquely not variable;it's a class.
It's also not an array. Empty [] have very limited meaning. If you
are using them, you are almost always doing the wrong thing.
Let's not get carried away. I use them all the time, e.g. when
I have a constant array, and I want the compiler to calculate
the size.

I'm sure you know it, but "std::string[]" is a legal type name,
it's an array of unspecified dimensions of std::string. It's
not very useful, and I can't think of a context where I'd want
to use that type name directly, but there are contexts where
declaring variables of the type might make sense, e.g.:
extern std::string idTable[] ;

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 29 '07 #9
On Apr 29, 9:05 am, Gianni Mariani <gi3nos...@mariani.wswrote:
>
Try this:
vector< vector<string v = vector< vector<string[](4);
This is a syntax error. It should be:

vector< vector<string v(4);
Apr 29 '07 #10
Thanks for all the help guys.

--Shafik

Apr 30 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
10
by: dalbosco | last post by:
Hello, I am new to STL and I've written the following code which crash. Could anyone tell me why this code crash? Thanks by advance. -- J-F #include <iostream>
4
by: misirion | last post by:
Ciao, vorrei poter scrivere e leggere su files binari dei vettori di stringhe, ed avrei implementato questo codice: ifstream fin(conf.c_str(),ios::binary); char inc; fin.read( inc,...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
5
by: Gary Wessle | last post by:
whats an efficient way to copy a string to a vector<string>? how about this? #include <iostream> #include <string> #include <vector> Using namespace std;
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
5
by: Peithon | last post by:
Hi, I'm trying to create a vector of strings and print the contents using an iterator but I'm getting an error with my very first string. Can anyone help?
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.