Vector of vectors, how to find size?

Apr 15, 2013 at 3:03pm
How would I edit this to show the size of the inner vector? (Not sure if that is technically correct. Please correct me if it's not)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<vector<int>> test {{2,3,4,5}, {7,8,9}};

    for (int x = 0; x < test.size(); ++x)
    {
        cout << "Outer Loop" << endl;

        for (int y = 0; y < test.size(); ++y)
        {
            cout << test[x][y] << endl;
        }
    }

return 0;
}
Last edited on Apr 15, 2013 at 3:10pm
Apr 15, 2013 at 3:10pm
To get the size of vector test[x] you do test[x].size().
Apr 15, 2013 at 3:14pm
1
2
3
4
5
6
7
test.size(); // size of the outer vector

// size of the inner vector at position 0

test[0].size(); // or
test.at(0).size(); // or
(*i).size(); // where i is an iterator to test[0] 


Nice to see you use C++11 elements!
Apr 15, 2013 at 3:14pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

using namespace;

int main()
{
    vector<vector<int>> test {{2,3,4,5}, {7,8,9}};

    for ( const auto &v : test )
    {
        cout << "Inner Loop" << endl;

        for ( int x : v )
        {
            cout << x << ' ';
        }
        std::cout << std::endl;
    }

    return 0;
}


In your code there shall be

for (int y = 0; y < test[x].size(); ++y)

instead of

for (int y = 0; y < test.size(); ++y)
Apr 15, 2013 at 3:17pm
closed account (3qX21hU5)
First when you declare vectors of vectors do it like so vector< vector<int> > // Hint: Notice the space between the >'s


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector< vector<int> > test {{2,3,4,5}, {7,8,9}};

    // Prints the first elements size
    // Which means it prints the size of the vector inside the fist element
    cout << test[0].size();

    return 0;
}


You have to remember that each element of the first vector is a vector itself and has all the functionality of a normal vector. So you just need to tell the program which element you want to access the size of and then use the size command.

EDIT: A better example based on your code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector< vector<int> > test {{2,3,4,5}, {7,8,9}};

    for (auto i = 0; i != test.size(); ++i)
    {
        cout << "Numbers in Element: " << i << endl;
        for (auto j = 0; j != test[i].size(); ++j)
            cout << test[i][j] << " ";

        cout << endl;
    }

    return 0;
}


DOUBLE EDIT: Gezz I'm always to slow :(
Last edited on Apr 15, 2013 at 3:21pm
Apr 15, 2013 at 3:25pm
Zereo wrote:
First when you declare vectors of vectors do it like so vector< vector<int> > // Hint: Notice the space between the >'s

Is this a stylistic suggestion? Because in C++11 it's not an error to not use spaces here.
Apr 15, 2013 at 3:26pm
@Zereo
First when you declare vectors of vectors do it like so vector< vector<int> > // Hint: Notice the space between the >'s


Why shall it be done as you showed?
Apr 15, 2013 at 3:27pm
*it is not an error to exclude the space.
Apr 15, 2013 at 3:39pm
I'm in the midst of the Brain Trust! Thanks people for all the answers =).

@Catfish3 - I'm learning from a C++11 updated book so yeah I'm up with the times! Unless that's sarcasm, then =(. Thanks for the code example. Yours and vlad's helped me figure out how to set up iterators instead. And Peter87 nudged on that.

@Zereo - Yeah the book warned me about the spacing, but it refers to older compilers. Appreciate the explanation, hits the nail in the coffin.
Last edited on Apr 15, 2013 at 3:40pm
Apr 15, 2013 at 3:42pm
closed account (3qX21hU5)
Is this a stylistic suggestion? Because in C++11 it's not an error to not use spaces here.
*it is not an error to exclude the space.
Why shall it be done as you showed?


Pre C++11 it is a error though so might as well cover your corners. It is a error because Pre C++11 the compiler mistook the two >>'s to mean the >> operator.

Also I guess it doesn't have to be done as shown but a lot of people don't have C++11 compilers and it would be in error for them.
Last edited on Apr 15, 2013 at 3:45pm
Apr 15, 2013 at 3:43pm
If you use any C++11 code you can assume all your code need not worry about C++03 anymore.
Apr 15, 2013 at 3:47pm
closed account (3qX21hU5)
That is true LB, so I guess the proper way of phrasing what I said should have been "Be warned though that if you are not using C++11 you need to declare it like so" instead of "you should declare it like so". My mistake for miss wording it.
Apr 15, 2013 at 3:55pm
@Zereo
Pre C++11 it is a error though so might as well cover your corners. It is a error because Pre C++11 the compiler mistook the two >>'s to mean the >> operator


It is not serious because using of '>" is not compatible with the previous standard also in some other cases. So there is no any sense to do what you suggested.

Next time do not give bad advices.
Apr 15, 2013 at 3:59pm
@ Olysold: No sarcasm, I'm genuinely excited when I see people write C++11 code.
Apr 15, 2013 at 4:01pm
closed account (3qX21hU5)
Next time do not give bad advices.


Ummm... I already admitted I probably phrased it wrong, but I don't see how telling someone that vector<vector<int>> can be in error for older compilers is giving bad advice.

Also have no idea what you are trying to say by
It is not serious because using of '>" is not compatible with the previous standard also in some other cases. So there is no any sense to do what you suggested.

Apr 15, 2013 at 4:08pm
#Catfish3 - Awesome. Hopefully that's enough to draw you into my threads in the future when I ask questions.
Apr 15, 2013 at 5:56pm
@Zereo
Also have no idea what you are trying to say by

It is not serious because using of '>" is not compatible with the previous standard also in some other cases. So there is no any sense to do what you suggested.


I said very clear that there are other incompatibiles of using < and > in specifying template arguments between the previous and the current C++ Standards.
Apr 15, 2013 at 6:05pm
closed account (3qX21hU5)
Oh ok now I understand.

And sorry if I sounded rude I just don't really like to be told that I'm giving bad advice when in fact I did no such thing. Specially in that manner. Anyways just wanted to say sorry.
Topic archived. No new replies allowed.