我正在尝试编译以下代码:
\n#include <map>\n#include <condition_variable>\n\nclass MyClass\n{\npublic:\n MyClass(): m_cv() {}\n std::condition_variable m_cv; //just to illustrate that a copy constructor is not obvious to write\n};\n\nint main()\n{\n std::map<std::string,MyClass> testmap;\n testmap.emplace("key",MyClass());\n}\nRun Code Online (Sandbox Code Playgroud)\n编译在最后一行失败(emplace方法调用)失败,并出现一个很长的错误,如下所示:
error: no matching function for call to \xe2\x80\x98std::pair, MyClass>::pair(const char [4], MyClass)\nRun Code Online (Sandbox Code Playgroud)\n通过反复试验,我了解到错误来自于缺少MyClass. 如果我添加一个,或者如果我用基本类型替换条件变量属性(int例如),错误就会消失。然而我不确定我的分析,最重要的是,我不明白为什么复制构造函数MyClass这里需要一个复制构造函数。
所以我的问题是双重的:
\n阅读节点句柄的文档,我注意到节点句柄类型的许多功能可以简单地通过std::unique_ptr. 事实上,节点句柄类型的功能与std::unique_ptr. 它只有一个更符合关联容器特性的接口,例如key_type和mapped_type别名以及获取键/映射值引用的函数。
std::unique_ptr因此,与as的简单特化相比,该标准引入了节点句柄类型,还有其他优点吗node_type?
如果我构建我自己的二叉树,那么我可以找到每个节点的深度.示例代码如下
template<class datatype>
void binary_node<datatype>::printNodeWithDepth(int currentNodeDepth)
{
if ( left )
left->printNodeWithDepth(currentNodeDepth+1);
std::cout << value << " and the depth is " << currentNodeDepth << std::endl;
if ( right)
right->printNodeWithDepth(currentNodeDepth+1);
}
Run Code Online (Sandbox Code Playgroud)
但是想知道,因为map是b-tree,是否有可能为此写一些类似的东西std::map?
我想vector使用指针插入新元素我有以下示例代码:
struct info {
string Name;
int places; // i will use the binary value to identfy the visited places example 29 is 100101
// this means he visited three places (London,LA,Rome)
vector<int> times; // will represent the visiting time,e.g. 1,2,5 means london 1 time, LA
// twice and Rome five times
};
map<string,vector<info> *> log;
Run Code Online (Sandbox Code Playgroud)
Peaple来自不同的城市,我将检查城市是否存在,只需将新人添加到其中vector,否则创建一个新的地图对象:
vector<info> tp;
info tmp;
if(log.size()==0|| log.count(city)==0) //empty or not exist
{
tp.push_back(tmp);
vector<info>* ss = new vector<info>;
ss=&(tp);
// …Run Code Online (Sandbox Code Playgroud) std::map<std::string, Obj> myMap;
std::set<std::string> mySet;
Run Code Online (Sandbox Code Playgroud)
我想删除那些myMap没有键的对mySet.
我该怎么做?我找到了std::remove_if算法,但似乎不适用于此.
我有一张地图如下:
std::map<int, std::unique_ptr<Person>> ratingMap;
Run Code Online (Sandbox Code Playgroud)
我想创建一个函数,它接受一个字符串参数_name并遍历地图,直到它找到一个具有相同名称的人:
void Person::deleteFromMap(const std::string& _name){
//Searches the map for a person whose name is the same as the argument _name
auto found = std::find(ratingMap.begin(), ratingMap.end(),
[&](const std::unique_ptr<Person>& person) -> bool{return person->getName() == _name; });
Run Code Online (Sandbox Code Playgroud)
但是,这拒绝编译并给出以下错误:
错误1错误C2678:二进制'==':找不到哪个运算符带有'std :: pair'类型的左操作数(或者没有可接受的转换)
我已经花了将近两个小时尝试其中的变体以试图让它工作,因为我在过去编写了类似的lambda函数,这些函数已按预期编译和工作.为什么会这样?
我正在研究一段代码,其目标是成为一个快速的"搜索引擎".我在一个文件中有条目,需要在读取整个文件后进行搜索.它们需要可以通过条目的名称进行搜索,并且它从文件的开头偏移.我的问题是内存使用问题,因为有数百万条目.目前我使用两个单独的std :: maps来存储数据,以便可以指定任一搜索项.这导致数据的双重存储,这正是我试图减少的.
我已经使用valgrind massif来发现内存使用的主要部分是条目的双重存储.
目前的储存方法:
struct entry {
std::string name;
uint16_t offset;
uint16_t size;
bool isConst;
};
nameSearchMap.insert(std::pair<std::string, entry>(s_entry.name, e_entry));
offsetSearchMap.insert(std::pair<uint16_t, SymInfo>(s_entry.offset, s_entry));
Run Code Online (Sandbox Code Playgroud)
有没有办法可以制作一个可以通过任何一种键搜索的地图?
以下代码的预期行为是什么,
#include <map>
...
std::map<int, A *> myMap;
myMap[0];
if(myMap[0] == NULL) {// true or false?
}
Run Code Online (Sandbox Code Playgroud)
if语句会评估为true吗?
我想知道std::map在julia中是否有任何等价物......仅仅为了映射的目的,我知道我可以使用Pair类型,例如,但是其他类型是否具有更好的功能std::map?
Map包含字符串作为键,A作为值的对象.来自std :: map/std :: unordered_map的函数clear()不会调用析构函数.当我想要清除地图时,我必须照顾自己的记忆.它只是一种使用for循环来处理内存的方法吗?码:
#include <iostream>
#include <unordered_map>
class A
{
public:
A() { std::cout << "A()" << std::endl; }
~A() { std::cout << "~A()" << std::endl; }
};
int main ()
{
std::unordered_map<std::string, const A*> mymap = { {"house",new A()}, {"car", new A()}, {"grapefruit", new A()} };
//mymap.clear();//won`t call destructor
for(const auto &i : mymap)
delete i.second; //dealocate value
mymap.clear(); //clear whole object from tha map
}
Run Code Online (Sandbox Code Playgroud)
是否可以更快地完成此操作,例如不使用for循环?