C++ 当我的代码给出正确的输出时,为什么会出现分段错误? #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main(){ /*在此处输入代码。从标准输入读取输入。将输出打印到标准输出*/ int n; cin>>n; mapm; 字符串名; int选择,标记; 而(n>0) { cin>>选择>>名称; 如果(选项==1) { cin>>标记; map::迭代器itr=m.find(名称); 如果(itr==m.end()) { m、 插入(配对(名称、标记)); } 否则{ itr->second=itr->second+标记; } } else if(选项==2) { map::迭代器itr=m.find(名称); itr->second=0; } else if(选项==3){ map::迭代器itr=m.find(名称); cout

C++ 当我的代码给出正确的输出时,为什么会出现分段错误? #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main(){ /*在此处输入代码。从标准输入读取输入。将输出打印到标准输出*/ int n; cin>>n; mapm; 字符串名; int选择,标记; 而(n>0) { cin>>选择>>名称; 如果(选项==1) { cin>>标记; map::迭代器itr=m.find(名称); 如果(itr==m.end()) { m、 插入(配对(名称、标记)); } 否则{ itr->second=itr->second+标记; } } else if(选项==2) { map::迭代器itr=m.find(名称); itr->second=0; } else if(选项==3){ map::迭代器itr=m.find(名称); cout,c++,C++,您的程序的输入是什么 当您访问内存位置以外的内存时,会发生分段错误 我怀疑选项2和选项3随时都可能导致分段错误。 因为即使输入名称不在映射中,您也在尝试更改其值。 它不安全。您给程序提供的是什么输入导致了此崩溃?请编辑您的问题以添加此信息。它在哪里崩溃?在什么输入上?当选项为3时,您有:cout #include <cmath> #include <cstdio> #include <vector> #include <iostream> #inc

您的程序的输入是什么

当您访问内存位置以外的内存时,会发生分段错误

我怀疑选项2和选项3随时都可能导致分段错误。 因为即使输入名称不在映射中,您也在尝试更改其值。
它不安全。

您给程序提供的是什么输入导致了此崩溃?请编辑您的问题以添加此信息。它在哪里崩溃?在什么输入上?当选项为3时,您有:
cout
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    
    int n;
    cin>>n;
    map<string,int>m;
    string name;
    int choice, marks;
    while(n>0)
    {
        cin>>choice>>name;
        if(choice==1)
        {
            cin>>marks;
            map<string,int>::iterator itr=m.find(name);
            if(itr==m.end())
            {
              m.insert(make_pair(name,marks)); 
               
            }
            else{
                itr->second=itr->second+marks;
               
            }

        }
        
        else if(choice==2)
        {
            map<string,int>::iterator itr=m.find(name);
             itr->second=0;
        }
        
        else if(choice==3){
            map<string,int>::iterator itr=m.find(name);
            cout<<itr->second<<endl;
        }
        
        n--;
        
    }  
    return 0;
}