Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c++;地图?_C++_Maps - Fatal编程技术网

C++ 如何在c++;地图?

C++ 如何在c++;地图?,c++,maps,C++,Maps,我正在做一个需要唯一键和值的项目,所以我决定使用地图。除了有人可能想要更改键值的情况外,一切都正常。我不知道为什么,但它会导致碎片故障。我不能这样做吗 void Journal::set_id(int id){ // journal class if(join.count(id)){ // join is: static map <int,string> join cout<<"J

我正在做一个需要唯一键和值的项目,所以我决定使用地图。除了有人可能想要更改键值的情况外,一切都正常。我不知道为什么,但它会导致碎片故障。我不能这样做吗

void Journal::set_id(int id){               // journal class
    if(join.count(id)){                     // join is: static map <int,string> join
        cout<<"Journal ID is already being used. Try again."<<endl;
    }
    else {
        join.erase (join.find(id));
        join.insert(pair<int,string>(id,name));
    }
}
void Journal::set_id(int id){//Journal类
if(join.count(id)){//join是:静态映射连接
你的逻辑有缺陷

void Journal::set_id(int id){
    if(join.count(id)){
        cout<<"Journal ID is already being used. Try again."<<endl;
    }
    else {
        // When you hit this block of the code, there is nothing
        // in the map corresponding to 'id'.
        // join.find(id) returns the same iterator as join.end()
        // Calling erase() with that iterator is causing you the
        // problem.

        // Don't call erase. Just insert the new item.
        // join.erase (join.find(id));
        join.insert(pair<int,string>(id,name));
    }
}
void Journal::set_id(int id){
if(join.count(id)){
你的逻辑有缺陷

void Journal::set_id(int id){
    if(join.count(id)){
        cout<<"Journal ID is already being used. Try again."<<endl;
    }
    else {
        // When you hit this block of the code, there is nothing
        // in the map corresponding to 'id'.
        // join.find(id) returns the same iterator as join.end()
        // Calling erase() with that iterator is causing you the
        // problem.

        // Don't call erase. Just insert the new item.
        // join.erase (join.find(id));
        join.insert(pair<int,string>(id,name));
    }
}
void Journal::set_id(int id){
if(join.count(id)){
你的逻辑有缺陷

void Journal::set_id(int id){
    if(join.count(id)){
        cout<<"Journal ID is already being used. Try again."<<endl;
    }
    else {
        // When you hit this block of the code, there is nothing
        // in the map corresponding to 'id'.
        // join.find(id) returns the same iterator as join.end()
        // Calling erase() with that iterator is causing you the
        // problem.

        // Don't call erase. Just insert the new item.
        // join.erase (join.find(id));
        join.insert(pair<int,string>(id,name));
    }
}
void Journal::set_id(int id){
if(join.count(id)){
你的逻辑有缺陷

void Journal::set_id(int id){
    if(join.count(id)){
        cout<<"Journal ID is already being used. Try again."<<endl;
    }
    else {
        // When you hit this block of the code, there is nothing
        // in the map corresponding to 'id'.
        // join.find(id) returns the same iterator as join.end()
        // Calling erase() with that iterator is causing you the
        // problem.

        // Don't call erase. Just insert the new item.
        // join.erase (join.find(id));
        join.insert(pair<int,string>(id,name));
    }
}
void Journal::set_id(int id){
if(join.count(id)){

cout您刚刚检查以确保
id
未被用作映射中的键。如果是,则发出错误。因此,现在您知道
id
不在映射中。如果
id
不在映射中,
join.find(id)
将返回
join.end()
,因此您实际上根本不需要调用
查找
。但更重要的是,您随后调用了
join.erase(join.end())
,这是一个错误

见:

迭代器pos必须有效且可取消引用。因此end()迭代器(有效但不可取消引用)不能用作pos的值


您刚刚检查以确保
id
未被用作映射中的键。如果是,则发出错误。因此,现在您知道
id
不在映射中。如果
id
不在映射中,
join.find(id)
将返回
join.end()
,因此您实际上根本不需要调用
查找
。但更重要的是,您随后调用了
join.erase(join.end())
,这是一个错误

见:

迭代器pos必须有效且可取消引用。因此end()迭代器(有效但不可取消引用)不能用作pos的值


您刚刚检查以确保
id
未被用作映射中的键。如果是,则发出错误。因此,现在您知道
id
不在映射中。如果
id
不在映射中,
join.find(id)
将返回
join.end()
,因此您实际上根本不需要调用
查找
。但更重要的是,您随后调用了
join.erase(join.end())
,这是一个错误

见:

迭代器pos必须有效且可取消引用。因此end()迭代器(有效但不可取消引用)不能用作pos的值


您刚刚检查以确保
id
未被用作映射中的键。如果是,则发出错误。因此,现在您知道
id
不在映射中。如果
id
不在映射中,
join.find(id)
将返回
join.end()
,因此您实际上根本不需要调用
查找
。但更重要的是,您随后调用了
join.erase(join.end())
,这是一个错误

见:

迭代器pos必须有效且可取消引用。因此end()迭代器(有效但不可取消引用)不能用作pos的值


您可以通过插入项,然后检查返回值以查看插入是否成功(如果该键已存在,则不会成功),从而简化代码,而不是检查该键是否存在并仅在未找到时插入它

void Journal::set_id(int id){
if(!(join.insert(std::make_pair(id,name)).second))

cout您可以简化代码,只需插入项,然后检查返回值以查看插入是否成功(如果该键已存在,则不会成功),而不是检查该键是否存在,并仅在未找到时插入它

void Journal::set_id(int id){
if(!(join.insert(std::make_pair(id,name)).second))

cout您可以简化代码,只需插入项,然后检查返回值以查看插入是否成功(如果该键已存在,则不会成功),而不是检查该键是否存在,并仅在未找到时插入它

void Journal::set_id(int id){
if(!(join.insert(std::make_pair(id,name)).second))

cout您可以简化代码,只需插入项,然后检查返回值以查看插入是否成功(如果该键已存在,则不会成功),而不是检查该键是否存在,并仅在未找到时插入它

void Journal::set_id(int id){
if(!(join.insert(std::make_pair(id,name)).second))

coutI的意思是说“某人可能想在哪里更改密钥”。什么是
日志
加入
?我们需要所有的事实来正确回答!另外:使用“编辑”进行编辑按钮,而不是在注释中;)这对代码有帮助吗?考虑到上下文,我认为人们可以找到答案。不过,一般来说,让人们知道是个好主意-当人们知道所有对象是什么时,你会得到更好、更快的响应。你想删除什么键值对?我的意思是“有人可能想在哪里更改密钥”.什么是
日志
加入
?我们需要所有事实来正确回答!另外:使用“编辑”按钮进行编辑按钮,而不是在注释中;)这对代码有帮助吗?考虑到上下文,我认为人们可以找到答案。不过,一般来说,让人们知道是个好主意-当人们知道所有对象是什么时,你会得到更好、更快的响应。你想删除什么键值对?我的意思是“有人可能想在哪里更改密钥”。什么是
日志
加入
?我们需要所有的事实来正确回答!另外:使用“编辑”按钮进行编辑,而不是在注释中;)这对代码有帮助吗?给定