Not very efficient. A copy of the whole vector is created upon return and if the vector is huge, performance suffers. Possibly pass in by reference to a function sounds better.
e.g
func(vector<string>&)
Edit: I see OP want a function with return type. Well then vector<string> func(); should suffice.
Okay, I'm trying to compile and run a program using these various aspects of 2D vectors. I'm going to break the errors down into individual snippets of questions... Any help would be greatly appreciated!
1) Any idea why this would be throwing the following error? string & s = "";
invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'const char*'
In the cpp file for this class, func is trying to directly access private member vect... Any ideas why the compiler is throwing a scope resolution error for vect?
3) Suppose in the same class as above, there is a member function int rows();. Why does it throw this error:
non-member function 'int rows()' cannot have cv-qualifier
I'm not understanding the first one...
I need s to be a reference; I'm passing it into a function that will change its value and I would rather save memory by not sending it by value. Is there no way to do this? Also, could you explain a bit more how I'm initializing it to a const object?
StringTable::StringTable(ifstream & infile)
{
string s = "";
int i = 0;
vect.push_back(vector<string>());
while (infile)
{
if (readMultiWord(s,infile,"#"))
vect[i].push_back(s);
else
vect.push_back(vector<string>());
}
}
Is this the right way to dynamically create 2D vectors? Ignore scope errors and assume these are all included and such (they are snippets of a bigger code and I'm lazy ;P )
Also assume the file has already been opened with
1 2
ifstream infile;
infile.open("file.txt");
The file.txt data is formatted as follows:
word # word # word word # #
word word # word # word # #
#
So I would want the vector's first row's first vector to contain the string "word", second "word", third "word word", and then the second row's first vector "word word", "word", "word", and so on and so forth.
Not very efficient. A copy of the whole vector is created upon return and if the vector is huge, performance suffers. Possibly pass in by reference to a function sounds better.