std::map满足容器的要求([map.overview] p2)。
容器需要满足以下条件:
Run Code Online (Sandbox Code Playgroud)b.begin()结果:
iterator;const_iterator为常数b.返回:引用容器中第一个元素的迭代器。
复杂性:恒定。
这对我来说似乎不切实际。Astd::map通常被实现为自平衡二叉搜索树,通常需要对数复杂度才能找到迭代必须开始的最左边节点。
您将如何begin()在恒定时间内实施?标准库的实现实际上符合这一点吗?
最初我的代码看起来像这样:
std::map< std::pair<int,int>, std::vector<Class0*> > aMap;
Run Code Online (Sandbox Code Playgroud)
有效.现在我的代码看起来像这样:
std::map< std::pair<Vec3f, Vec3f>, std::vector<Class0*> > aMap;
Run Code Online (Sandbox Code Playgroud)
它不再正确映射(编译正常).为什么?我该如何解决这个问题?
编辑:这里的流行需求是3D矢量(3个浮点数)的比较代码:
class Vec3f {
...
bool operator () ( const Vector3f& v0, const Vector3f& v1 ) const {
return std::tie(v0[0], v0[1], v0[2]) < std::tie(v1[0], v1[1], v1[2]);
} ...
Run Code Online (Sandbox Code Playgroud)
从这个问题重载运算符集.上面的比较适用于一组,但显然不适合一对.为什么?
在 Visual Studio 2013 终极版中:
一种)
using namespace std;
typedef map<string, double> my_set;
Run Code Online (Sandbox Code Playgroud)
=> 错误:地图不是模板
b)
typedef std::map<string, double> my_set;
Run Code Online (Sandbox Code Playgroud)
=> 错误:不允许使用限定名称
a 或 b 在文件 foo.h 中,两个版本都不起作用。我究竟做错了什么?(是的,花了一些时间谷歌搜索 => 没有结果,c++ 是一种奇怪的语言)
我试图控制我正在开发的程序的工作流程.为此,我有一个map< unsigned int, list < unsigned int > >第一个键将是id,第二个键(列表)将用于知道我是否正确结束所有任务.我在此列表中使用的唯一操作是:
myMap[iD].size()
myMap[iD].push_back(foo) <- ( foo is an unsigned int )
for (std::list<unsigned int>::iterator it=myMap[iD].begin(); it != myMap[iD].end(); ++it){
myMap[iD].erase(it)
}
Run Code Online (Sandbox Code Playgroud)
我的地图长度可以增长到1452个元素,每个元素列表大小可以从1000到5000的数量级.
当我运行程序时,有时会收到分段错误,有时会出现错误的分配错误.我的猜测是,这来自push_back,因为:
这是我使用地图的代码中唯一的部分:
if (FOO != 0){
if (PID != 0){
if ( myMap.size() + 5 < myMap.max_size()){
if (myMap[PID].size() > 1000) myMap[PID].pop_front();
myMap[PID].push_back(EVENTVALUE);
}
}
} else {
if (PID != 0 and foo2 != 0 and myMap.find(PID) != myMap.end()) {
for (std::list<unsigned int>::iterator it=myMap[PID].begin(); …Run Code Online (Sandbox Code Playgroud) 我正在std::map使用外部for循环和嵌套for循环迭代两次.我希望在满足条件时停止内循环.我知道,如果我打电话给休息,它将停止两个循环.我不希望这样.我只想停止内for循环.
我的代码段如下:
for(auto markerCounter = ellipsePropertiesMap.begin(); markerCounter != ellipsePropertiesMap.end(); markerCounter++)
{
for(auto markerCounter2 = ellipsePropertiesMap.begin(); markerCounter2 != ellipsePropertiesMap.end(); markerCounter2++)
{
if(conditionMet)
std::advance(markerCounter2, ellipsePropertiesMap.size());
//I also tried the following:
//markerCounter2 = ellipsePropertiesMap.end();
//markerCounter2++;
}
}
Run Code Online (Sandbox Code Playgroud)
我试图通过使用std :: map的大小推进迭代器来停止内部for循环,但是它不起作用.我也试图得到结束迭代器并推进它以结束循环.显然这也不起作用.
那么,我怎样才能真正停止内循环呢?
谢谢.
我在写一些C++代码时遇到了以下现象:
我有一张看起来像这样的地图:
std::map<test_struct_t*, unsigned int, cmp_by_value> testmap;
Run Code Online (Sandbox Code Playgroud)
该映射位于我的程序中,结构定义为:
struct test_struct_t {
int x; int y; int val;
bool operator<(const test_struct_t &o) const {
return x < o.x || y < o.y || val < o.val;
}
test_struct_t(int a, int b, int c) : x(a), y(b), val(c) {}
};
Run Code Online (Sandbox Code Playgroud)
我写的自定义比较器是:
struct cmp_by_value {
bool operator()(const test_struct_t *a, const test_struct_t *b) const
{
return *a < *b;
}
};
Run Code Online (Sandbox Code Playgroud)
现在,在我的主要方法中,我执行以下操作:
testmap.insert({new test_struct_t(0, 0, 2 ), 6});
testmap.insert({new test_struct_t(0, 1, 2 ), 6}); …Run Code Online (Sandbox Code Playgroud) 我正在努力学习如何std::map工作,我有以下问题:
int id; // stores some id
struct stuff {
std::vector<int> As;
std::vector<int> Bs;
} stuff;
std::map<int, stuff> smap;
void foo () {
int count = 2;
int foo_id = 43;
for (int i = 0; i < count; count++) {
stuff.As.push_back(count);
stuff.Bs.push_back(count);
}
smap.insert(foo_id, stuff);
}
Run Code Online (Sandbox Code Playgroud)
目前我得到:
error: type/value mismatch at argument 2 in template parameter list for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
std::map<int, stuff> smap;
error: request for member ‘insert’ in …Run Code Online (Sandbox Code Playgroud) 所以我用地图和对测试一些东西,我遇到了问题.
std::map<std::string, int> pairTest;
pairTest.insert(std::make_pair("Peter", 100));
for (std::map<std::string, int>::iterator it = pairTest.begin(); it != pairTest.end(); it++) {
std::cout << it->first << ":" << it->second << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我怎么能把"彼得"变成丹尼尔?我本该能够做到吗?
我怎样才能使对永远不变,并添加不同价值的相同密钥?
我尝试过另一种方式,这是一种更正确的做法吗?
std::map<std::string, int> pairTest;
pairTest.insert(std::pair<std::string, int>("Peter", 100));
for (std::map<std::string, int>::iterator it = pairTest.begin(); it != pairTest.end(); it++) {
std::cout << it->first << ":" << it->second << std::endl;
}
Run Code Online (Sandbox Code Playgroud) 我试图从具有两个以上元素的std :: map中访问特定元素.这是一个例子:
std::map <int, CString, CString, CString> map;
//Initialise
map[0] = _T("stuff1"), _T("stuff2"), _T("stuff3");
//now if I just want to access stuff3 is it this:
CString str = map[0][2];
//or something more like this?
CString str = map[0]. ???
Run Code Online (Sandbox Code Playgroud)
任何帮助都会非常感谢.
编辑:感谢抱歉,第一次使用地图,我想知道为什么我找不到有关更多元素的std :: map的任何信息.
我需要使用关联数组,当听说STL std::map我决定使用它时,我有以下代码.
map<string, string> aArray;
aArray["First"] = "William";
aArray["Second"] = "James";
aArray["Third"] = "Michael";
aArray["Forth"] = "Jayden";
aArray["Fifth"] = "Ashley";
for(std::map<string, string>::iterator it=aArray.begin();it!=aArray.end();++it){
cout << it << endl;
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何制作有效的循环.
我在其他教程中看到过如下:
cout << it->first << endl;
cout << it->second << endl;
Run Code Online (Sandbox Code Playgroud)
但也有指定的任何成员first,second.
还有一个错误:
no match for 'operator<<' (operand types are 'std::ostream {aka
std::basic_ostream<char>}' and 'std::map<std::basic_string<char>,
std::basic_string<char> >::iterator {aka
std::_Rb_tree_iterator<std::pair<const std::basic_string<char>,
std::basic_string<char> > >}')请向我解释我该怎么做?
我有以下地图用于存储客户名称和使用其代码和描述选择的itens字典,例如:
Name Item Code, Item Description
Customer1 -> 1234, Item 1
1233, Item 2
1232, Item 3
Run Code Online (Sandbox Code Playgroud)
所以我使用以下结构:
std::map<std::string, std::map<std::string, std::string>> purchase_list;
Run Code Online (Sandbox Code Playgroud)
现在我需要查找现有名称和现有项目代码.
要查找现有名称:
if (purchase_list.find(purchase_list.begin(), purchase_list.end(), "SearchName") != purchase_list.end())
std::cout << "SeachName found!!" << std::endl;
Run Code Online (Sandbox Code Playgroud)
对我来说这应该工作,但我在编译时遇到以下错误:
no matching function for call to ‘std::map<std::basic_string<char>, std::map<std::basic_string<char>, std::basic_string<char> > >::find(std::map<std::basic_string<char>, std::map<std::basic_string<char>, std::basic_string<char> > >::iterator, std::map<std::basic_string<char>, std::map<std::basic_string<char>, std::basic_string<char> > >::iterator, std::string&)’ search_name) == purchase_list.end())
Run Code Online (Sandbox Code Playgroud)
我要改变"SearchName"格式吗?这里的语法是什么?
要搜索商品代码,我会重复相同的代码,寻找客户找到的条目...
谢谢你的帮助......
我试过这个:
std::map<int,int> m;
Run Code Online (Sandbox Code Playgroud)
它起作用了——m变成一张空地图。m但如果编译器选择默认不初始化为空映射,则此方法可能不起作用。更好的解决方案?