Vectors of vectors (2D vector)

I have a few questions.

1) Is it possible to have a prototype for a function that returns a vector of strings? If so, how would you write the return type?

2) How to I use the pop_back() method to add a 'row' to a 2D vector? (i.e. add another vector to a 2D vector

3) What is the proper declaration for a 2D vector? Would it just be
vector<vector<string> > vect; ?
1) Certainly.
vector<string> func();

2) pop_back() doesn't add a row; you're thinking of push_back().
1
2
3
4
vector<vector<string> > two_d;
vector<string> one_d;
// ... do stuff with one_d
two_d.push_back(one_d);

3) That's right.
For some reason, this part of my code won't compile correctly:

vect.push_back(vector<string>);
1) Certainly.
vector<string> func();


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.
Last edited on
Yes, it is a reference vector. :)
@TC: That code isn't compiling because you aren't pushing an actual vector. It fails for the same reason this fails:
1
2
vector<int> vect;
vect.push_back(int); // <- what int? 
Try this:
 
vect.push_back(vector<string>()); // adding () constructs a const vector<string> 
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*'


2) Suppose I have the following in a header file:
1
2
3
4
5
6
7
8
#include <vector>
using namespace std;

public:
void func();

private:
vector<vector<string> > vect;


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
Last edited on
1) You can't initialize a non-const reference to a const object. Try const string& s = ""; or don't use a reference at all: std::string s = "";

2) You can't use public: or private: outside of a class definition.

3) The function has to be a member of a class. You need to qualify it with the class name:
int MyClass::rows();
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?
You can pass a normal object into a function that takes a reference it it will be modified.
1
2
3
4
5
6
7
8
9
10
void change(int& a) {
    a += 2;
}

int main() {
    int x = 3;
    change(x);
    // x will now be 5
    return 0;
}
Okay, one more thing.

Suppose:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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>());
    }
}


and

1
2
3
4
5
6
7
8
9
10
11
12
13
bool readMultiWord(string & s, ifstream & infile, const string & sep)
{
	if (infile.peek() == sep[0])
	{
		infile.ignore();
		return false;
	}
	else
	{
		getline(infile,s,sep[0]);
		return true;
	}
}


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.
Last edited on
1) Certainly.
 
vector<string> func();



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.


Nonsense. Ever heard of RVO?
Last edited on
Topic archived. No new replies allowed.