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

S

Shafik

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
 
G

Gianni Mariani

Shafik said:
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);
 
S

Salt_Peter

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;
 
K

Kouisawang

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.
 
R

Rolf Magnus

Shafik said:
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);
 
S

Salt_Peter

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::eek:stream&
operator<<( std::eek:stream& 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 said:
(10, 1.1));
std::vector< std::vector< int > > vvd(10, std::vector< int >(100, 0));
 
R

Ron Natalie

Kouisawang said:
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.
 
J

James Kanze

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.)
 
J

James Kanze

Kouisawang said:
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[] ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,975
Members
47,533
Latest member
medikillz39

Latest Threads

Top