[英]Mapping an int to a struct of vectors in C++
我正在嘗試了解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);
}
目前我得到:
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);
我希望能夠將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.
結果,沒有名為std::map<int, stuff>
使用的名為stuff
類型。
您可以通過重命名結構類型來解決此問題:
struct stuff_t {
std::vector<int> As;
std::vector<int> Bs;
} stuff;
std::map<int, stuff_t> smap;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.