2016-12-22 68 views
0

如何訪問指向結構中的向量的指針的值?我有以下代碼:訪問指向結構中的向量的指針

#include <iostream> 
#include <vector> 

using namespace std; 

struct item { 
    int value; 
    vector<bool> pb; 
    vector<bool> *path = &pb; 
}; 

int main(int argc, char* argv[]) { 
    vector<item> dp(10); 
    for (int n = 0; n < 10; n++) 
     dp[n].pb = vector<bool>(10); 

    if (dp[1].path[2] == true) 
     cout << "true"; 
    else cout << "false"; 
} 

這將導致以下編譯錯誤:

Error C2678 binary '==': no operator found which takes a left-hand operand of type 'std::vector<bool,std::allocator<_Ty>>' (or there is no acceptable conversion) 

我如何可以訪問存儲在DP值[1]。路徑[2]?

+2

與從指針訪問不在結構中的向量的值相同的方式。 – juanchopanza

回答

2

路徑是指向矢量的指針。你必須做到以下幾點,它指向

if ((*(dp[1].path))[2] == true) 

if (dp[1].path->operator[](2) == true) 
+0

感謝@WhozCraig指出了這一點 – user3286661

1

向量訪問值另外,你可以使用atfunction這也是

checks whether n is within the bounds of valid elements in the vector

代碼示例

if (dp[1].path->at(2) == true)