访问指针到结构中的向量
Accessing pointer to a vector in a struct
我如何访问指针对向量构成中的值的值?我有以下代码:
#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] .path [2]?
路径是指向向量的指针。您必须执行以下操作以访问指向
的向量中的值if ((*(dp[1].path))[2] == true)
或
if (dp[1].path->operator[](2) == true)
此外,您可以使用
的at
函数检查n是否在向量
中的有效元素的范围内
代码示例
if (dp[1].path->at(2) == true)
相关文章:
- 将结构向量排序为子组
- 访问存储在向量C++中的结构的多态成员
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 如何在 c++ 中创建结构向量的映射
- 带unique_ptr的结构向量
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 将 int 映射到 C++ 中的向量结构
- 如何在 c++ 中的向量结构中使用结构向量
- 无法将值存储到向量结构中
- 无法将.CSV文件读取为代表向量结构的类
- MPI,C,派生类型,向量结构
- 向量结构的打印成员
- 在 c++ 中对向量<结构 S> 进行二分搜索?
- 不需要删除向量<结构A*>
- 在C++中循环向量结构<string>
- 将字符串放入字符串的向量结构中
- 共享内存中的向量结构
- 我的向量结构没有像预期的那样迭代