提问者:小点点

C++不能通过指向向量的向量的指针循环


我试图通过一个指针循环到一个ints向量的向量,并获得嵌套向量中int的值,结果遇到了一些问题。 这是我的代码:

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";
    }
}

这是我运行时终端中的输出:

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

这不是我在我的程序中使用的实际代码,它是一个简化的问题。

这里有几个问题,都和指针有关

  1. 为什么v3_size的第一个值不是2,而是3?
  2. 为什么v3_size最终输出为疯狂数字
  3. 您可以看到我注释掉了一个嵌套循环的代码块,但我需要它来工作。 它迭代嵌套向量中的int,我需要访问这些int.

谢谢你的帮助!


共1个答案

匿名用户

你的问题在这里:

v3_size = pointer_to_test_production[v2].size();

pointer_to_test_production是一个指针,而不是一个向量,您需要取消引用它。

请尝试以下操作:

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

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|不能通过|指向|向量|向量|指针|循环)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?