簡體   English   中英

C++ 無法循環通過指向向量向量的指針

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

我正在嘗試遍歷指向整數向量的向量的指針,並獲取嵌套向量中的整數值,但遇到了問題。 這是我的代碼:

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

這是我運行時終端中的 output:

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

這不是我在我的 prog 中使用的實際代碼,它是一個簡化的問題。

所以我在這里遇到了幾個問題,它們都與指針有關

  1. 為什么 v3_size 的第一個值不是 2 而不是 3?
  2. 為什么 v3_size 最終 output 是一個瘋狂的數字
  3. 你可以看到有一段代碼我注釋為嵌套循環,但我需要它來工作。 它遍歷嵌套向量中的整數,我需要訪問這些整數。

謝謝你的幫助!

你的問題在這里:

v3_size = pointer_to_test_production[v2].size();

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

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM