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