简体   繁体   中英

C++ Can't Loop Through Pointer to Vector of Vectors

I'm trying to loop through a pointer to a vector of a vector of ints and get the value of the int in the nested vector, and am running into problems. This is my code:

void test() {
    
    // base vectors
    std::vector<int> test_vect1 = { 1, 2, 3 };
    std::vector<int> test_production_item = { 1, 2 };
    std::vector<std::vector<int>> test_production;
    test_production.push_back(test_production_item);
    test_production.push_back(test_production_item);
    test_production.push_back(test_production_item);
    
    // pointers
    std::vector<int> *pointer_to_test_vect1;
    int *pointer_to_int_in_test_vect1;
    std::vector<std::vector<int>> *pointer_to_test_production;
    
    // looping
    size_t v1;
    size_t v1_size = 10;
    size_t v2;
    size_t v2_size;
    size_t v3;
    size_t v3_size;
    
    pointer_to_test_vect1 = &test_vect1;
    v1_size = pointer_to_test_vect1->size();
    
    std::cout << "v1_size=" << v1_size << "\n";
    
    for ( v1 = 0; v1 < v1_size; v1++ ) {
        
        std::cout << "LOOP1\n";
        std::cout << "v1=" << v1 << "\n";
        
        pointer_to_int_in_test_vect1 = &(*pointer_to_test_vect1)[v1]; // 1... then 2... then 3...
        std::cout << "value in pointer_to_int_in_test_vect1=" << *pointer_to_int_in_test_vect1 << "\n";
                
        pointer_to_test_production = &test_production; // [ [ 1, 2 ], [ 1, 2 ], [ 1, 2 ] ]
        v2_size = pointer_to_test_production->size(); // 3
        std:: cout << "v2_size=" << v2_size << "\n";
        
        for ( v2 = 0; v2 < v2_size; v2++ ) {
            
            std::cout << "LOOP2\n";
            std::cout << "v2=" << v2 << "\n";
            
            v3_size = pointer_to_test_production[v2].size();
            
            // should be 2 but reads as 3? then some crazy numbers i assume are addresses
            std::cout << "v3_size=" << v3_size << "\n";
            
            // ERROR
            // for ( v3 = 0; v3 < v3_size; v3++ ) {
                //if ( pointer_to_test_production[v2][v3] == 1 ) {
                   // std::cout << "Success!";
                //}
            // }
            
            std::cout << "DONE LOOP2\n";
        }
        
        std::cout << "DONE LOOP1\n";
    }
}

This is the output in the terminal when I run it:

v1_size=3
LOOP1
v1=0
value in pointer_to_int_in_test_vect1=1
v2_size=3
LOOP2
v2=0
v3_size=3                         <---- should be 2?
DONE LOOP2
LOOP2
v2=1
v3_size=18446738210098829165      <---- wtf?
DONE LOOP2
LOOP2
v2=2
v3_size=12297829382472464075      <-----wtf
DONE LOOP2
DONE LOOP1
LOOP1
v1=1
value in pointer_to_int_in_test_vect1=2
v2_size=3
LOOP2
v2=0
v3_size=3
DONE LOOP2
LOOP2
v2=1
v3_size=18446738210098829165
DONE LOOP2
LOOP2
v2=2
v3_size=12297829382472464075
DONE LOOP2
DONE LOOP1
LOOP1
v1=2
value in pointer_to_int_in_test_vect1=3
v2_size=3
LOOP2
v2=0
v3_size=3
DONE LOOP2
LOOP2
v2=1
v3_size=18446738210098829165
DONE LOOP2
LOOP2
v2=2
v3_size=12297829382472464075
DONE LOOP2
DONE LOOP1

This isnt actual code I'm using in my prog, its a problem simplified.

So there's a couple problems i'm having here, and its all related to pointers

  1. why isnt the first value for v3_size 2 instead of 3?
  2. why does v3_size eventually output as a crazy number
  3. you can see there's a block of code i commented out as a nested loop, but i need that to work. its iterating through the ints in the nested vector, and i need to access those ints.

Thanks for your help!

Your problem is here:

v3_size = pointer_to_test_production[v2].size();

pointer_to_test_production is a pointer not a vector and you need to dereference it.

Try this:

v3_size = (*pointer_to_test_production)[v2].size();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM