[英]Copying C++ Map into key and value vectors
我有一個map
,我想要第一列, ie (*it).first
。首先被推回到一個向量然后(*it)->second
被推回到另一個向量
這是最好的方法嗎?
std::vector<std::string>test;
for ( it=mymap.begin() ; it != mymap.end(); it++ )
{
test.push_back((*it).first);
}
我的另一個問題是,如果我有一個循環,即我怎么會插入所有整數i
到(*it).first
?
for(int i = 0; i < 10; i++)
{
// 1 - 10 will go in (*it).first
}
我想有一些整數(*it).first
,並有相關的價值觀(*it).second;
使用std::transform
。
首先定義兩個函數key
和value
,它們分別取出一對字符串並返回第一個或第二個值。
#include <map>
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
const std::string& key(const std::pair<std::string, std::string>& keyValue)
{
return keyValue.first;
}
const std::string& value(const std::pair<std::string, std::string>& keyValue)
{
return keyValue.second;
}
然后使用std::transform
從<algorithm>
與所述功能到地圖轉變成任一vector
的鍵或一個vector
值。
int main()
{
using namespace std; // be explicit normally, trying to be brief here
map<string, string> contacts;
contacts["alice"] = "555-2701";
contacts["bob"] = "555-2702";
vector<string> keys(contacts.size());
vector<string> values(contacts.size());
transform(contacts.begin(), contacts.end(), keys.begin(), key);
transform(contacts.begin(), contacts.end(), values.begin(), value);
cout << "Keys:\n";
copy(keys.begin(), keys.end(), ostream_iterator<string>(cout, "\n"));
cout << "\n";
cout << "Values:\n";
copy(values.begin(), values.end(), ostream_iterator<string>(cout, "\n"));
return 0;
}
輸出:
Keys:
alice
bob
Values:
555-2701
555-2702
你的第一個問題,“如何將我的地圖的第一列推入一個向量,將第二列推入另一個向量”得到解決:
std::map<std::string, std::string> mymap;
std::vector<std::string> keys;
std::vector<std::string> values;
for ( std::map<std::string,std::string>::iterator it=mymap.begin() ; it != mymap.end(); ++it )
{
keys.push_back(it->first);
values.push_back(it->second);
}
你的第二個問題,“怎么會插入所有整數i
到(*it).first
?” 這樣解決了:
std::map<int, int> mymap2;
for(int i = 0; i < 10; i++)
{
// Insert default value into map
// This sets '(*it).first' to 'i' and
// '(*it).second' to a default value (in
// this case, 0).
mymap2[i];
}
要么
std::map<int, int> mymap3;
for(int i = 0; i < 10; i++)
{
// Insert specified value into map
// this sets '(*it).first' to 'i', and
// '(*it).second' to the value returned from the function.
maymap3[i] = ChooseSpecificValue(i);
}
好吧,它可以通過一個簡單的循環來完成:
for (auto const& p: mymap) {
vec1.push_back(p.first);
vec2.push_back(p.second);
}
或者使用std::transform
算法,雖然它在這里非常詳細:
std::transform(mymap.begin(), mymap.end(), std::back_inserter(vec1),
[](MyMap::const_reference p) { return p.first; });
假設你已經將你的地圖聲明為字符串鍵和值(即map<string, string> mymap;
那么它就像下面一樣,也假設你已經將'it'變量聲明為map<string, string>::iterator it
等等:
std::vector<std::string> test;
std::vector<std::string> second;
std::map<string, string>::iterator it;
for ( it=mymap.begin() ; it != mymap.end(); it++ )
{
test.push_back((*it).first);
second.push_back((*it).second);
}
不確定你的下一個問題。
而我的另一個問題是,如果我有一個循環,即如何將所有整數插入(*它).first?
在std::map
的情況下,你不能修改返回的迭代器...... std::map
鍵/值對數據結構中的鍵成員(即第一個)被有意指定為常量值,並在std::map
數據結構中的鍵/值對的生命周期的開始處初始化為其常量值。 如果鍵不是常量,則在更改鍵時最終會造成嚴重破壞,因為std::map
中的節點假定按鍵排序。 鍵/值對數據結構的第二個成員是可以更改的成員。
因此,如果要在地圖中插入一組鍵/值對,則可以執行以下操作:
std::map<int, int> mymap;
int some_other_value = 100;
for (int i=0; i < 10; i++)
{
mymap[i] = some_other_value++;
}
問題的第一部分:
std::vector<std::string> test;
std::vector<std::string> test2; // assuming map is from string to string
for (it = mymap.begin(); it != mymap.end(); ++it)
{
test.push_back(it->first); // push first in one vector
test2.push_back(it->second); // push second in another vector
}
所以,是的,一個簡單的可以做你想要的。
問題的第二部分:
由於您要更新地圖的鍵,因此需要將其從地圖中刪除並插入更改的地圖。 所以:
std::string first, second;
first = it->first;
second = it->second;
mymap.erase(it); // be careful with invalidating iterator
// change first
mymap[first] = second;
要改變first
通過將所有整數i
給它,這真的取決於類型first
。 例如,使用字符串,您可能意味着這樣的事情:
ostringstream sout;
for (int i = 0; i < 10; ++i)
sout << (i?" ":"") << i;
first = sout.str();
或者,如果首先是例如一個set
,您可能意味着這樣的事情:
for (int i = 0; i < 10; ++i)
first.insert(i);
它將是一個迭代器,它將指向map中的一個位置,並且最多只有一個迭代器的第一個和第二個值。 在最大值,您可以使用多個鍵或相同的鍵,保持相同/不同的值,具體取決於鍵/值組合。
至於在地圖中按鍵的向量中推送值是值得關注的,你可以像按下鍵一樣進行
std::vector<std::string>test;
std::vector<std::string>test2;
for ( it=mymap.begin() ; it != mymap.end(); it++ )
{
test.push_back((*it).first);
test2.push_back((*it).second);
}
你的問題還不清楚。
如果你想在地圖中處理不同的數據類型,我會模擬一個通用的復制函數:
template <class A, class B>
void mycopy(std::map<A, B>&m, std::list<A>& keys, std::list<B>& values) {
typename std::map<A, B>::iterator it;
for (it = m.begin(); it != m.end(); ++it) {
keys.push_back( (*it).first );
values.push_back( (*it).second );
}
}
把它混合起來:
std::map<int, std::string> mymap;
std::list<int> keys;
std::list<std::string> values;
mymap[1] = "string1";
mymap[2] = "string2";
mycopy(mymap, keys, values);
std::map<std::string, int> mymap1;
std::list<std::string> keys1;
std::list<int> values1;
mymap1["string1"] = 1;
mymap1["string2"] = 2;
mycopy(mymap1, keys1, values1);
編輯:是__copy不是最好的定義。 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.