当datact = 10736尝试插入unordered_map时,会发生分段错误(请参阅调用行,该调用行将调用该错误).请参阅下面的尝试修复.
当抛出SIGSEGV时,它指向我的第764行 hashtable_policy.h
INPUT:数据文件,其中column1 = count,column2 = 16个字符的字符串
目的:通过将1-替换不同序列的所有计数加在一起来聚类16个字符的序列.看到的第一个序列是"起源",通过它来识别其所有的1个替换朋友.
PSEUDOCODE:对于文件中的每一行:
读取计数,读取序列.
如果序列key_value存在于散列'location'(类型unordered_map)中,则添加当前计数;
否则创建一个新的key_value,让它指向此处的计数,并指定所有1个替换序列也指向此计数.
码:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include "matrix.h"
using namespace std;
int nuc2num(char currchar)
{ // returns 0,1,2,3 for A,C,T,G respectively
int outnum;
if (currchar=='A')
{
outnum=0;
}
else if (currchar=='C')
{
outnum=1;
}
else if (currchar=='T')
{
outnum=2;
}
else
{
outnum=3;
}
return outnum;
}
int main(int argc, char* argv[])
{
//command …Run Code Online (Sandbox Code Playgroud) 我得到了以下示例代码
// unordered_map::find
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,double> mymap = {
{"mom",5.4},
{"dad",6.1},
{"bro",5.9} };
std::string input;
std::cout << "who? ";
getline (std::cin,input);
std::unordered_map<std::string,double>::const_iterator got = mymap.find (input);
if ( got == mymap.end() )
std::cout << "not found";
else
std::cout << got->first << " is " << got->second;
std::cout << std::endl;
return 0;
Run Code Online (Sandbox Code Playgroud)
当我尝试在Windows 7上使用VS 2010编译它时,我得到编译时错误(虽然它看起来不错)
1>\testing.cpp(13): error C2552: 'mymap' : non-aggregates cannot be initialized with initializer list
1> 'std::tr1::unordered_map<_Kty,_Ty>' : Types with a …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用std :: unordered_map,如此处的示例所示.
class CSVRecord {
public:
CSVRecord(string csvLine) : _fields(vector<string>()) {...}
vector<string> _fields;
};
int main(int argc, char* argv[]) {
unordered_map<string, CSVRecord> m;
CSVRecord rec = CSVRecord("test");
m["t"] = rec;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,m["t"] = rec 给出一个错误:no matching function for call to ‘CSVRecord::CSVRecord()’.
我使用了m.insert(pair<string, CSVRecord>("t",rec)),但我想知道为什么原来不起作用.
我很困惑为什么静态unordered_map被清除如果我通过引用得到它但不是如果我通过指针得到它...(你可以在这里执行代码:http://cpp.sh/4ondg)
是因为当引用超出范围时,它的析构函数会被调用吗?如果是这样,那么第二个获取功能会得到什么?
class MyTestClass {
public:
static std::unordered_map<int, int>& getMap() {
static std::unordered_map<int, int> map;
return map;
}
static std::unordered_map<int, int>* getMapByPointer() {
static std::unordered_map<int, int> map;
return ↦
}
};
int main()
{
// By reference
{
auto theMap = MyTestClass::getMap();
std::cout << theMap.size() << std::endl;
theMap[5] = 3;
std::cout << theMap.size() << std::endl;
}
{
auto theMap = MyTestClass::getMap();
std::cout << theMap.size() << std::endl;
theMap[6] = 4;
std::cout << theMap.size() << std::endl;
}
// …Run Code Online (Sandbox Code Playgroud) 我注意到insert函数std::unordered_map返回a std::pair.
std::pair显示值是否真正插入的第二个元素.但是,我对此感到困惑.可以std::unordered_map通过哈希映射实现的a在插入时失败吗?什么时候会发生?
这是cppreference的描述:
返回值
1-2)返回一个由插入元素的迭代器(或阻止插入的元素)组成的对,以及表示插入是否发生的bool.
我有一个带有字符串键的无序映射和一个包含三个字符串和一个int的元组.如何访问单个元组来设置它们.
鉴于:
std::unordered_map<string,std::tuple<string, string, string,int>> foo_data_by_username;
Run Code Online (Sandbox Code Playgroud)
如何设置say的单个元组值 foo_data_by_username[some_user];
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循环?
我正在尝试手动将一些Java移植到C++中.
Java:
public Class Item {
public String storage = "";
public Item(String s, int tag) { storage = s; }
...
}
public class ProcessItems {
Hashtable groups = new Hashtable();
void save(Item w) { groups.put(w.storage, w); }
}
Run Code Online (Sandbox Code Playgroud)
我的C++:
#include<iostream>
#include<unordered_map>
#include<string>
class Item {
public:
std::string storage;
Item(std::string s, int tag) { storage = s; }
...
}
class ProcessItems {
public:
std::unordered_map<std::string, std::string> *groups = new std::unordered_map<std::string, std::string>();
void save(Item w) { groups.insert(w::storage, w); }
... …Run Code Online (Sandbox Code Playgroud) 我想弄清楚使用unordered_map自定义类的一些要点.下面是我用来练习一个简单类的练习代码Line.我很困惑,为什么插入Line2in main()不会使程序输出insert failed当mfor Line1和的值Line2都是3.请注意,因为我只比较第一值(即m中)operator==功能class Line,从而Line1与Line2在此代码应该有相同的密钥.不应该插入已存在的密钥无效吗?有人可以向我解释原因吗?谢谢!
#include<iostream>
#include<unordered_map>
using namespace std;
class Line {
public:
float m;
float c;
Line() {m = 0; c = 0;}
Line(float mInput, float cInput) {m = mInput; c = cInput;}
float getM() const {return m;}
float getC() const {return c;}
void setM(float mInput) {m = mInput;}
void setC(float cInput) …Run Code Online (Sandbox Code Playgroud) 在的情况下,unordered_map我们定义了hash和pred每当我们使用仿函数user-defined键。
地图的模板语法如下:
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;
Run Code Online (Sandbox Code Playgroud)
如果是map,则没有hashand predfunctors选项。我们永远不会发生碰撞的情况map。如果发生冲突,那为什么不使用hash和pred函数unordered_map呢?我在这里想念什么吗?