C++ map<string, vector<char> > 访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8960550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ map<string, vector<char> > access
提问by MCP
I've created a map of vectors that looks like this:
我创建了一个看起来像这样的矢量地图:
map<string, vector<char> > myMap;
string key = "myKey";
vector<char> myVector;
myMap[key] = myVector;
I want to be able to append 'char's' to the vector in the map but I can't figure out how to access said vector to append once the particular key/value(vector) has been created. Any suggestions? I'm iterating over char's and might be adding a lot to the vector as I go so it would be nice to have a simple way to do it. Thanks.
我希望能够将“字符”附加到地图中的向量,但我无法弄清楚如何访问所述向量以在创建特定键/值(向量)后进行附加。有什么建议?我正在迭代 char 并且可能会在我进行时向向量添加很多内容,因此如果有一种简单的方法来做到这一点会很好。谢谢。
I would like the vector in map to be appended as I go. I don't need the original vector...I just need to return the map of key/vector's that I've created (after apending) so that I can pass it to another function. What does the * in map* > do? Is that refrencing a pointer? (I haven't gotten there in lecture yet) Also, do I need: myMap[key]->push_back('s'); or myMap[key].push_back('s'); ??
我希望在我走的时候附加地图中的矢量。我不需要原始向量...我只需要返回我创建的键/向量的映射(附加后),以便我可以将它传递给另一个函数。地图 * > 中的 * 有什么作用?那是引用指针吗?(我还没有在讲座中到达那里)另外,我需要:myMap[key]->push_back('s'); 或 myMap[key].push_back('s'); ??
回答by Fred Foo
To append:
附加:
myMap[key].push_back('c');
Or use myMap.find
, but then you have to check whether you get an end
iterator. operator[]
returns a reference to the vector
.
或者使用myMap.find
,但是您必须检查是否获得了end
迭代器。operator[]
返回对vector
.
But this modifies the vector
stored in the map
, not the original one, since you've stored a copy in the map
with myMap[key] = myVector;
. If that's not what you want, you should rethink your design and maybe store (smart) pointers to vectors in your map.
但这会修改vector
存储在 中的map
,而不是原始的,因为您已经在map
with 中存储了一个副本myMap[key] = myVector;
。如果这不是您想要的,您应该重新考虑您的设计,并可能在地图中存储(智能)指向矢量的指针。
回答by Meysam
Given you know the key:
鉴于你知道关键:
string key = "something";
char ch = 'a'; // the character you want to append
map<string, vector<char> >::iterator itr = myMap.find(key);
if(itr != myMap.end())
{
vector<char> &v = itr->second;
v.push_back(ch);
}
you could also use the map::operator[]
to access the map entry, but if the key does not exist, a new entry with that key will be created:
您也可以使用map::operator[]
来访问映射条目,但如果该键不存在,则会创建一个具有该键的新条目:
vector<char> &v = myMap[key]; // a map entry will be created if key does not exist
v.push_back(ch);
or simply:
或者干脆:
myMap[key].push_back(ch);
回答by Michael Chinen
To access the mapped value, which in your case is a vector, you just supply the key in square brackets like you did to assign the value. So, to append 'a':
要访问映射值(在您的情况下是向量),您只需在方括号中提供键,就像分配值一样。因此,要附加 'a':
myMap[key].push_back('a');
回答by Mir Milad Hosseiny
I have an new suggestion. You can use vector<char>*
instead of vector<char>
in order to collect pointer of vectors in your map
. For more information see the bellow code:
我有一个新建议。您可以使用vector<char>*
代替vector<char>
来收集map
. 有关更多信息,请参阅以下代码:
map<string, vector<char>* > myMap;
string key = "myKey";
vector<char>* myVector = new vector<char>();
myMap[key] = myVector;
myMap[key]->push_back('S');