C++ 带错误的循环向量退出过程结束,退出代码为-1073741819

C++ 带错误的循环向量退出过程结束,退出代码为-1073741819,c++,loops,vector,C++,Loops,Vector,乐:万一有什么关系,我用的是克莱恩。该程序不打印任何内容 我需要处理一个向量,该向量包含name card\u type形式的字符串向量,并按字母顺序显示所有卡片以及每个卡片的实例数。输出应该类似于: a_card 1 mastercard 4 visa 3 到目前为止,我所拥有的代码,以错误退出过程,以退出代码-1073741819完成。问题是什么 #include <iostream> #include <vector> #include <string>

乐:万一有什么关系,我用的是克莱恩。该程序不打印任何内容

我需要处理一个向量,该向量包含
name card\u type
形式的字符串向量,并按字母顺序显示所有卡片以及每个卡片的实例数。输出应该类似于:

a_card 1
mastercard 4
visa 3
到目前为止,我所拥有的代码,以错误退出
过程,以退出代码-1073741819完成。问题是什么

#include <iostream>
#include <vector>
#include <string>

void stats(const std::vector<std::vector<std::string>>& vec) {
    std::vector<std::pair<std::string, int>> orderedCards;

    for (const auto& client : vec){ //scan database for cards and make ordered vector of pairs
        if (orderedCards.empty()){ //insert first pair to empty vector
            orderedCards.push_back(std::make_pair(client[1],1));

        }else{ //for each new client scan vector and add new client/increment counter
        
           for (auto pair = orderedCards.begin(); pair != orderedCards.end(); ++pair){
               if (client[1][0] < pair->first[0]){ //compare first letter of new client and existing client
                   orderedCards.insert(pair, std::make_pair(client[1], 1));// if its smaller, insert in front
                   break;// break iteration and go for next client

               } else if (client[1] == pair->first){ //if theyre the same, increment counter
                   pair->second+=1;
                   break;

               } else if (pair+1 == orderedCards.end()) //if end is reached, there was no existing client thus add new one with high letter
                   orderedCards.push_back(std::make_pair(client[1], 1));
           }
        }
    }
    
    for (const auto& count : orderedCards)
        std::cout<<count.first<<" "<<count.second<<std::endl; //print on each line card and counter

}

std::vector<std::vector<std::string>> dataBase;


int main()
{
    dataBase={{"name", "bankcard"},{"name", "visa"},{"name", "bankcard"},{"name", "mastercard"},{"name", "bankcard"},{"name", "visa"}};
    
    stats(dataBase);

    return 0;
}
#包括
#包括
#包括
无效统计(常数标准::向量和向量){
std::向量有序卡片;
对于(const auto&client:vec){//扫描数据库中的卡,并生成对的有序向量
if(orderedCards.empty()){//将第一对插入到空向量
orderedCards.push_-back(std::make_-pair(客户机[1],1));
}else{//为每个新的客户端扫描向量添加新的客户端/增量计数器
对于(自动配对=orderedCards.begin();配对!=orderedCards.end();++pair){
如果(客户端[1][0]first[0]){//比较新客户端和现有客户端的第一个字母
insert(pair,std::make_pair(client[1],1));//如果较小,则在前面插入
break;//中断迭代并转到下一个客户机
}否则,如果(客户机[1]==pair->first){//如果它们相同,则递增计数器
配对->第二+=1;
打破
}else if(pair+1==orderedCards.end())//如果到达end,则不存在现有客户机,因此添加具有高字母的新客户机
orderedCards.push_-back(std::make_-pair(客户机[1],1));
}
}
}
用于(常数自动计数和计数:订购卡)

std::cout多亏了@yaodav,我从迭代器切换到了map和排序alg。更新了代码并进行了解释:

#include <iostream>
#include <vector>
#include <string>

bool compare(const std::pair<std::string , int>& pair1, const std::pair<std::string , int>& pair2){
    return pair1.first[0] < pair2.first[0];
}

void stats(const std::vector<std::vector<std::string>>& vec) {
        std::map<std::string, int> map;

    for (const auto& client : vec){ //scan database for cards and make map
        auto ret = map.insert({client[1], 1});// insert new key:value pair

        if (!ret.second) // this means that the key was already in the map
            ret.first->second+=1; // dereferance pointer to key to access value and +=1
    }

    std::vector<std::pair<std::string, int>> orderedCards(map.begin(), map.end()); // make vector of pairs (key, value)
    std::sort(orderedCards.begin(), orderedCards.end(), compare); // sort vector according to defined compare

    for (const auto& count : orderedCards)
        std::cout<<count.first<<" "<<count.second<<std::endl;

}

std::vector<std::vector<std::string>> dataBase;


int main()
{
    dataBase={{"name", "bankcard"},{"name", "visa"},{"name", "bankcard"},{"name", "mastercard"},{"name", "bankcard"},{"name", "visa"}};
    
    stats(dataBase);
#包括
#包括
#包括
布尔比较(常数std::pair&pair1,常数std::pair&pair2){
返回pair1.first[0]second+=1;//指向访问值和+=1的键的解除引用指针
}
std::vector orderedCards(map.begin(),map.end());//生成成对向量(键,值)
排序(orderedCards.begin(),orderedCards.end(),compare);//根据定义的比较对向量进行排序
用于(常数自动计数和计数:订购卡)

std::cout@xampierre这里有一个更简单的函数,不使用std::sort和算法库

void stats(const std::vector<std::vector<std::string>>& vec)
{

    std::map<std::string, int> orderedCards;
    for(auto & client : vec)
    {
        auto it = orderedCards.insert(std::make_pair(client[1],1));
        
        if (!it.second) // this means that the key was already in the map
            it.first->second+=1;
    }
    
   for (const auto& count : orderedCards)
        std::cout<<count.first<<" "<<count.second<<std::endl; //print on each line card and counter
}
void stats(const std::vector&vec)
{
std::地图订购卡;
用于(自动和客户端:vec)
{
autoit=orderedCards.insert(std::make_pair(客户机[1],1));
if(!it.second)//这意味着该键已在映射中
it.first->second+=1;
}
用于(常数自动计数和计数:订购卡)

你能用调试器运行你的代码吗?你喜欢的是'for(自动配对=orderedCards.begin();配对!=orderedCards.end();++pair)`在第二次迭代中,你可以看到你的代码,并在那里调试它。你的核心转储是因为你在使用迭代器,想想在插入一个元素后迭代器会发生什么?阅读关于ValidityThink的一节关于使用find函数或它的一些变体,或者你可以使用std::map,它的comperator使用的是字母顺序然后insert将按顺序插入这是您的当前输出代码?
银行卡3万事达卡1 visa卡3