多重地图值中的随机访问,或者我应该只使用矢量地图
Random Access in multimap's value, or should I just use map of vector
我想知道,是否有任何方法在multimap的值中执行随机访问。
#include <map>
#include <vector>
#include <string>
int main() {
std::map<std::string, std::vector<std::string>> m0;
m0["Food"].push_back("Ice Cream");
m0["Food"].push_back("Pizza");
// Random access to Pizza. Nice!
printf ("2nd Food is %sn", m0["Food"][1].c_str());
std::multimap<std::string, std::string> m1;
m1.insert(std::pair<std::string, std::string>("Food", "Ice Cream"));
m1.insert(std::pair<std::string, std::string>("Food", "Pizza"));
// Is there any way to perform random access in multimap?
std::multimap<std::string, std::string>::const_iterator find = m1.find("Food");
// Sequential access to Pizza. Bad :(
// I wish to have something
// printf ("2nd Food is %sn", find[1].c_str());
find++;
printf ("2nd Food is %sn", find->second.c_str());
getchar();
}
既然随机访问map的值是一个要求,那么使用map of vector更好吗?
std::multimap<>
的迭代器是严格双向的(§23.3.2/1),所以不,具有相同键的值之间的随机访问是不可能的。
相关文章:
- 我应该使用什么来代替void作为变体中的替代类型之一
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 我应该删除矢量<short>吗?
- 我应该如何修改此代码以使用给定字符串中的字母打印菱形图案
- 我应该在锁定TBitmap画布后解锁它吗
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 我应该避免多重实现继承吗
- 为了方便起见,我应该避免公开私有字段变量吗
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 违反const正确性:我应该现实地期待什么问题
- 我应该如何表示我拥有的连续元素序列?
- 我应该将除 .cpp 以外的其他文件添加到 git 中吗?
- 我应该为地图使用哪种数据结构?(C++)
- Eigen:我应该使用对齐地图进行密集计算吗?
- 如果我的密钥是我价值的一部分,我应该使用地图还是集合
- 我应该使用地图、3D 数组还是坚持使用堆栈
- 我应该为地图中的两个智能指针对制作自己的比较器吗?
- 多重地图值中的随机访问,或者我应该只使用矢量地图