I just want to hard code in a matrix using C++ (g++ 4.1.2), by default I went with a std::vector of std::vectors.
My guess is this can be done in one line I just don't know the correct syntax.
For example:
(1,2,5)
(9,3,6)
(7,8,4)
I thought it might be something like this -
vector<int> v1(1,2,3);
vector<int> v2(4,5,6);
vector<i开发者_如何学编程nt> v3(7,8,9);
vector<vector<int>> vA(v1,v2,v3);
Normally, I wold read this info out of a text file, but I need to manually put in the numbers by hand and I have to use g++ 4.1.2
If you're not going to change the size or shape of this matrix and since you're hard-coding the values anyway, you may be better with a plain old array:
int matrix[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Otherwise, Fred Nurk's answer is what you are looking for.
The simplest way is the easiest (without C++0x):
vector<vector<int> > v (3);
for (int a = 0; a != 3; ++a) {
v[a].resize(3);
for (int b = 0; b != 3; ++b) {
v[a][b] = a * 3 + b + 1;
}
}
With 0x initializers, which I doubt that version of gcc supports:
vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
精彩评论