C++ 将int映射到C++;
我正在尝试学习C++ 将int映射到C++;,c++,stdmap,C++,Stdmap,我正在尝试学习std::map的工作原理,我有以下问题: int id; // stores some id struct stuff { std::vector<int> As; std::vector<int> Bs; } stuff; std::map<int, stuff> smap; void foo () { int count = 2; int foo_id = 43; for (int i = 0; i < co
std::map
的工作原理,我有以下问题:
int id; // stores some id
struct stuff {
std::vector<int> As;
std::vector<int> Bs;
} stuff;
std::map<int, stuff> smap;
void foo () {
int count = 2;
int foo_id = 43;
for (int i = 0; i < count; count++) {
stuff.As.push_back(count);
stuff.Bs.push_back(count);
}
smap.insert(foo_id, stuff);
}
int-id;//存储一些id
结构材料{
std::向量As;
std::向量Bs;
}东西;
std::map-smap;
void foo(){
整数计数=2;
int foo_id=43;
对于(int i=0;i
目前我得到:
error: type/value mismatch at argument 2 in template parameter list for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
std::map<int, stuff> smap;
error: request for member ‘insert’ in ‘smap’, which is of non-class type ‘int’
smap.insert(int, stuff);
错误:“模板类std::map”的模板参数列表中参数2的类型/值不匹配
std::map-smap;
错误:请求“smap”中的成员“insert”,该成员为非类类型“int”
smap.插入(int,stuff);
我希望能够将
id
映射到struct
,该struct由for循环中填充的两个向量组成。我做错了什么?或者有更好的方法来映射这一点吗?struct stuff
将stuff
定义为struct
,但随后是}stuff代码>最后将stuff
重新定义为stuff
类型的变量
struct stuff { // stuff is a struct
std::vector<int> As;
std::vector<int> Bs;
} stuff; // stuff is now a variable of type stuff.
尝试重命名2个有效的东西中的一个!另外,插入是向地图添加内容的最佳方式吗?我该怎么做呢?@Blizzard如果是这样的话,你应该发布一个新问题。@Blizzard很难回答这个问题。最好的相对物是什么<代码>smap[foo_id]=东西代码>非常容易阅读<代码>smap.insert(foo_id,stuff)代码>可能会快一点<代码>安置
可以让您通过将所有内容构建到位来避免一些多余的任务和构建。
struct stuff_t {
std::vector<int> As;
std::vector<int> Bs;
} stuff;
std::map<int, stuff_t> smap;