banner



How To Get Size Of Vector C++

Vector of vectors, how to find size?

How would I edit this to prove the size of the inner vector? (Not sure if that is technically correct. Delight correct me if it's not)

                          1
2
iii
4
v
6
vii
viii
nine
x
11
12
13
14
15
xvi
17
eighteen
nineteen
20
21
                                                      #include <iostream>                            #include <vector>                            using                            namespace                            std;                            int                            chief() {     vector<vector<int>> test {{2,iii,iv,5}, {7,8,9}};                            for                            (int                            ten = 0; x < test.size(); ++ten)     {         cout <<                            "Outer Loop"                            << endl;                            for                            (int                            y = 0; y < test.size(); ++y)         {             cout << test[x][y] << endl;         }     }                            return                            0; }                        

Last edited on

To become the size of vector test[x] you do exam[x].size() .

                          1
two
3
4
5
six
vii
                          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]                                                                              

Squeamish to see you use C++xi elements!

                          one
two
3
4
5
6
seven
8
9
ten
11
12
13
14
15
16
17
18
nineteen
20
21
22
                                                      #include <iostream>                            #include <vector>                            using                            namespace;                            int                            master() {     vector<vector<int>> exam {{2,3,4,5}, {7,8,nine}};                            for                            (                            const                            auto                            &v : test )     {         cout <<                            "Inner Loop"                            << endl;                            for                            (                            int                            ten : five )         {             cout << x <<                            ' ';         }         std::cout << std::endl;     }                            return                            0; }                        

In your lawmaking there shall exist

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

instead of

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

First when you declare vectors of vectors do it like so vector< vector<int> > // Hint: Notice the space between the >'s

                          i
2
iii
4
five
6
7
eight
9
10
11
12
13
14
xv
                                                      #include <iostream>                            #include <vector>                            using                            namespace                            std;                            int                            main() {     vector< vector<int> > test {{two,three,4,five}, {7,8,ix}};                            // Prints the first elements size                            // Which means information technology prints the size of the vector within the fist element                            cout << test[0].size();                            render                            0; }                        

You lot accept to remember that each element of the first vector is a vector itself and has all the functionality of a normal vector. So you but demand to tell the programme which chemical element you want to admission the size of and and then use the size command.

EDIT: A ameliorate example based on your code

                          1
ii
3
4
5
6
7
viii
9
10
11
12
13
xiv
15
xvi
17
18
19
20
                                                      #include <iostream>                            #include <vector>                            using                            namespace                            std;                            int                            primary() {     vector< vector<int> > test {{2,iii,4,5}, {7,8,ix}};                            for                            (automobile                            i = 0; i != test.size(); ++i)     {         cout <<                            "Numbers in Element: "                            << i << endl;                            for                            (motorcar                            j = 0; j != test[i].size(); ++j)             cout << test[i][j] <<                            " ";          cout << endl;     }                            return                            0; }                        

DOUBLE EDIT: Gezz I'm always to wearisome :(

Terminal edited on

Zereo wrote:
First when you declare vectors of vectors do information technology like so vector< vector<int> > // Hint: Notice the space betwixt the >'s

Is this a stylistic suggestion? Because in C++11 it'south not an error to non use spaces here.

@Zereo
First when you declare vectors of vectors do it like and then vector< vector<int> > // Hint: Discover the space between the >'s

Why shall it exist done as you showed?

*it is not an mistake to exclude the space.

I'1000 in the midst of the Brain Trust! Thanks people for all the answers =).

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

@Zereo - Aye the book warned me nearly the spacing, but it refers to older compilers. Appreciate the explanation, hits the nail in the coffin.

Last edited on

Is this a stylistic proposition? Because in C++xi it's not an error to non apply spaces hither.

*information technology is not an error to exclude the infinite.

Why shall it be done as you showed?

Pre C++xi it is a error though so might also cover your corners. It is a error because Pre C++xi the compiler mistook the two >>'due south to hateful the >> operator.

Also I guess information technology doesn't have to exist done as shown but a lot of people don't accept C++11 compilers and information technology would be in error for them.

Final edited on

If you use any C++xi lawmaking you can assume all your lawmaking need not worry about C++03 anymore.

That is true LB, then I guess the proper mode of phrasing what I said should take been "Exist warned though that if you lot are not using C++eleven y'all demand to declare information technology like and so" instead of "you should declare information technology similar so". My mistake for miss diction it.

@Zereo
Pre C++11 it is a fault though so might every bit 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 uniform with the previous standard also in some other cases. So there is no whatever sense to do what you suggested.

Adjacent time practise not give bad advices.

@ Olysold: No sarcasm, I'm genuinely excited when I come across people write C++eleven code.

Next time practise not give bad advices.

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

Besides accept no idea what you are trying to say past

Information technology is not serious because using of '>" is not compatible with the previous standard also in some other cases. And then there is no whatsoever sense to exercise what y'all suggested.


#Catfish3 - Awesome. Hopefully that's enough to depict you into my threads in the future when I ask questions.

@Zereo
Also have no idea what y'all are trying to say by

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

I said very clear that at that place are other incompatibiles of using < and > in specifying template arguments between the previous and the current C++ Standards.

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. Particularly in that manner. Anyways just wanted to say sorry.

Topic archived. No new replies allowed.

How To Get Size Of Vector C++,

Source: https://www.cplusplus.com/forum/beginner/99123/

Posted by: lewisfran1950.blogspot.com

0 Response to "How To Get Size Of Vector C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel