访问结构中向量的指针

时间:2016-12-22 07:24:35

标签: c++ struct

如何访问结构中向量的指针值?我有以下代码:

angular.module("abc", ["ngRoute"])
        .config(function ($routeProvider) {   
            $routeProvider.when("/checkout", {
                templateUrl: "/checkoutPage.html",
                controller: "..."
            })
            .otherwise("/");
            $locationProvider.html5Mode(true);           
        });

导致以下编译错误:

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

如何访问存储在dp [1] .path [2]中的值?

2 个答案:

答案 0 :(得分:2)

path是指向矢量的指针。您必须执行以下操作才能访问它指向

的向量中的值
if ((*(dp[1].path))[2] == true)

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

答案 1 :(得分:1)

此外,您还可以使用at function

  

检查n是否在向量

中的有效元素范围内

代码示例

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