472,126 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 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 2367
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Matt Garman | last post: by
10 posts views Thread by dalbosco | last post: by
4 posts views Thread by misirion | last post: by
5 posts views Thread by Gary Wessle | last post: by
6 posts views Thread by arnuld | last post: by
5 posts views Thread by Peithon | last post: by
42 posts views Thread by barcaroller | last post: by
reply views Thread by leo001 | last post: by

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.