在我的C++应用程序中,我有一些值作为代码来表示其他值.要翻译代码,我一直在讨论使用switch语句或stl映射.开关看起来像这样:
int code;
int value;
switch(code)
{
case 1:
value = 10;
break;
case 2:
value = 15;
break;
}
Run Code Online (Sandbox Code Playgroud)
地图将是一个,stl::map<int, int>并且翻译将是一个简单的查找,其中代码用作键值.
哪一个更好/更有效/更清洁/接受?为什么?
最近,我被std :: map operator []函数搞糊涂了.在MSDN库中,它说:"如果找不到参数键值,那么它将与数据类型的默认值一起插入." 我试图更准确地搜索这个问题的解释.例如: std :: map default value 在这个页面中,Michael Anderson说"默认值是由默认构造函数(零参数构造函数)构造的".
现在我的任务是:"内置类型的默认值是什么?".编译器是否相关?或者c ++标准委员会是否有这个问题的标准?
我在visual studio 2008上对"int"类型进行了测试,发现"int"类型的构造值为0.
我正在尝试在地图中创建地图:
typedef map<float,mytype> inner_map;
typedef map<float,inner_map> outer_map;
Run Code Online (Sandbox Code Playgroud)
我能在内部地图中放置一些东西,或者iterator :: second会返回一个副本吗?
stl_pair.h建议后者:
74: _T2 second; ///< @c second is a copy of the second object
Run Code Online (Sandbox Code Playgroud)
但我的测试程序运行正常,代码如下:
it = my_map.lower_bound(3.1415);
(*it).second.insert(inner_map::value_type(2.71828,"Hello world!");
Run Code Online (Sandbox Code Playgroud)
那真相在哪里?这是副本吗?
我有以下代码,但我在最后一行收到错误:
struct coord {
int x, y;
bool operator=(const coord &o) {
return x == o.x && y == o.y;
}
bool operator<(const coord &o) {
return x < o.x || (x == o.x && y < o.y);
}
};
map<coord, int> m;
pair<coord, int> p((coord{0,0}),123);
m.insert(p); // ERROR here
Run Code Online (Sandbox Code Playgroud)
如何在地图中使用结构作为键?
我试图将代码更改为:
struct coord {
int x, y;
bool const operator==(const coord &o) {
return x == o.x && y == o.y;
}
bool const operator<(const coord &o) {
return …Run Code Online (Sandbox Code Playgroud) 我试图将自定义类型指定为std :: map的键.这是我用作键的类型.
struct Foo
{
Foo(std::string s) : foo_value(s){}
bool operator<(const Foo& foo1) { return foo_value < foo1.foo_value; }
bool operator>(const Foo& foo1) { return foo_value > foo1.foo_value; }
std::string foo_value;
};
Run Code Online (Sandbox Code Playgroud)
当与std :: map一起使用时,我收到以下错误.
error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Foo' (or there is no acceptable conversion) c:\program files\microsoft visual studio 8\vc\include\functional 143
Run Code Online (Sandbox Code Playgroud)
如果我改变下面的结构,一切都有效.
struct Foo
{
Foo(std::string s) : foo_value(s) {}
friend bool …Run Code Online (Sandbox Code Playgroud) 我有一个std :: map,我想使用子字符串搜索一个键.例如
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef std::map<std::string, std::string> TStrStrMap;
typedef std::pair<std::string, std::string> TStrStrPair;
int main(int argc, char *argv[])
{
TStrStrMap tMap;
tMap.insert(TStrStrPair("John", "AA"));
tMap.insert(TStrStrPair("Mary", "BBB"));
tMap.insert(TStrStrPair("Mother", "A"));
tMap.insert(TStrStrPair("Marlon", "C"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想搜索包含子串"Marl"而不是"Marlon"的位置.可能吗?怎么样?
编辑:没有加速库!
在std :: map中引用对象是否是线程安全的?
std::map< std::string, Object > _objects;
Run Code Online (Sandbox Code Playgroud)
map可以从许多线程更改,并且此访问是同步的,但引用只能从1个实例和线程访问的值(Object&).是否使用Object进行写操作是否安全,如果另一个线程将添加项目到映射?它会重新分配吗?
如何使用具有键值的std :: map容器降序.
例如,如果插入以下项目:
[2 , 5]
[1 , 34]
[3 , 67]
Run Code Online (Sandbox Code Playgroud)
他们将在地图上订购,如:
position 0: [1, 34]
position 1: [2, 5]
position 2: [3, 67]
Run Code Online (Sandbox Code Playgroud)
我可以反向迭代地图,但假设我下次插入[-1,60].它会被放在第一个位置吗?
如果存在std::map<std::pair<std::string, std::string>, some_type>找到其值的最佳方法是什么?
我想最明显的一个是做这样的事情:
但这将导致在对构造期间对和字符串map.find(std::make_pair(str1, str2));
进行复制构造。str1str2
我希望也许map.find(std::make_pair(std::ref(str1), std::ref(str2)));能有所帮助,但不幸的是没有,这仍然会产生字符串副本。
map.find(std::make_pair(std::move(str1), std::move(str2))应该可以工作,但我们假设这些字符串 ( str1, str2) 是 const 或者不应该移动。
所以我问是否有其他方法可以进行地图搜索而不进行多余的字符串副本?
(请注意,使用std::string_viewforstd::map key不是一个选项,因为地图应该拥有其字符串。)
我想继承std::map,但据我所知std::map,没有任何虚拟析构函数.
因此可以std::map在我的析构函数中显式调用析构函数以确保正确的对象破坏吗?
c++ ×10
stdmap ×10
dictionary ×2
std ×2
c++11 ×1
containers ×1
inheritance ×1
iterator ×1
map ×1
search ×1
sorting ×1