看来只有用vector< std::pair<std::string, std::string> >了
...
#include <vector>
#include <string>
#include <iostream>
int main(int argc, char** argv)
{
using namespace std;
typedef pair <std::string, std::string> spair__;
typedef vector< spair__ > vector_type;
vector_type svec;
svec.push_back( spair__( "January", "2004-01" ) );
svec.push_back( spair__( "Feburary", "2004-02" ) );
svec.push_back( spair__( "March", "2004-03" ) );
svec.push_back( spair__( "April", "2004-04" ) );
svec.push_back( spair__( "May", "2004-05" ) );
for ( vector_type::iterator it = svec.begin();
it != svec.end();
++it )
cout << it->first << " " << it->second << "/n";
return 0;
}
map的一种替代方案,不会自动排序
使用vector存储字符串对
最新推荐文章于 2025-09-12 12:44:50 发布
本文介绍了一种使用C++标准库中的vector容器来存储字符串对的方法。通过定义一个包含两个字符串元素的pair类型,并将其存储在vector中,实现了对月份名称与其对应年份的有序存储。文中展示了如何创建这样的vector、添加元素以及遍历输出所有存储的数据。
815

被折叠的 条评论
为什么被折叠?



