C++ C+中的复向量+;
我使用这个向量:C++ C+中的复向量+;,c++,vector,C++,Vector,我使用这个向量:向量。 我假设第一次迭代返回一个数组: for (vector<string, vector<int>>::iterator it = sth.begin(); it != sth.end(); ++it) { // how do I get the string? // I tried (*it)[0], but that did not work } for(vector::iterator it=sth.begin();it!=st
向量
。
我假设第一次迭代返回一个数组:
for (vector<string, vector<int>>::iterator it = sth.begin(); it != sth.end(); ++it) {
// how do I get the string?
// I tried (*it)[0], but that did not work
}
for(vector::iterator it=sth.begin();it!=sth.end();++it){
//我怎么才能拿到绳子?
//我尝试了(*it)[0],但没有成功
}
另外,我如何将_向后推
到此向量?传递vector()
对我不起作用。谢谢矢量拍摄:
- 第一个模板参数是一个类型
- 第二个可选参数是分配器
vector
不是字符串的有效分配器
假设您不想在此处绘制地图,您可能需要:
vector< pair<string, vector<int> > > outerVec;
vector<int> vecInt1, vecInt2;
vecInt1.push_back( 1 );
vecInt1.push_back( 5 );
vecInt2.push_back( 147 );
outerVec.push_back( std::make_pair( std::string("Hello World"), vecInt1 ) );
outerVec.push_back( std::make_pair( std::string("Goodbye Cruel World"), vecInt2 ));
向量取:
- 第一个模板参数是一个类型
- 第二个可选参数是分配器
vector
不是字符串的有效分配器
假设您不想在此处绘制地图,您可能需要:
vector< pair<string, vector<int> > > outerVec;
vector<int> vecInt1, vecInt2;
vecInt1.push_back( 1 );
vecInt1.push_back( 5 );
vecInt2.push_back( 147 );
outerVec.push_back( std::make_pair( std::string("Hello World"), vecInt1 ) );
outerVec.push_back( std::make_pair( std::string("Goodbye Cruel World"), vecInt2 ));
你想做什么?为什么向量被用作分配器?我在你的逻辑中遗漏了一些基本的东西……你不能用两种类型创建一个向量。你想要的似乎是一张地图。或者你需要创建一个std::pairs的向量这在几个方面都是错误的。你想解决什么问题?我正在模拟一个哈希映射行为,其中字符串是键名。但是它的工作方式有点不同,这就是为什么我一开始没有使用哈希映射。你想做什么?为什么向量被用作分配器?我在你的逻辑中遗漏了一些基本的东西……你不能用两种类型创建一个向量。你想要的似乎是一张地图。或者你需要创建一个std::pairs的向量这在几个方面都是错误的。你想解决什么问题?我正在模拟一个哈希映射行为,其中字符串是键名。但是is的工作方式有所不同,这就是为什么我没有首先使用哈希映射的原因。似乎我错过了
对。谢谢你,好像我错过了配对。谢谢
for( outer_vectype::const_iterator iter = outerVec.begin(),
iterEnd = outerVec.end();
iter != iterEnd; ++iter )
{
const pair_type & val = *iter;
std::cout << val.first;
for( inner_vectype::const_iterator inIter = val.second.begin(),
inIterEnd = val.second.end(); inIter != inIterEnd; ++inIter )
{
std::cout << '\t' << *inIter;
}
std::cout << '\n';
}
Hello World 1 5
Goodbye Cruel World 147