473,748 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2495
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*v ar;

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<s tring v(4);

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

vector<string*v ar;

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_iterat or 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*v ar;

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...@gma il.comwrote:
Do you want vector that store array of string, right?
Maybe this may work of you
vector<string*v ar;
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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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.n etwrote:
Kouisawang wrote:
Do you want vector that store array of string, right?
Maybe this may work of you
vector<string*v ar;
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::strin g[]" 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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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...@mari ani.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

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

Similar topics

1
18001
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 application using C++ and the STL for handling my data. Unfortunately, I must interact with a (vanilla) C API. I use vectors of strings (for simplicity and less memory hassle), but the function calls for this API require arrays of character...
10
3096
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
2051
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, sizeof(levelfiles) ); levelfiles = fromCharS(inc); fin.close();
2
2634
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 ? // test.cpp #include <iostream> #include <fstream> #include <vector> #include <string>
5
3551
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
5716
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 * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
5
15236
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
7381
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", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
42
4537
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 of string, in one line. I could
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.