循环向量退出并出现错误进程以退出代码 -1073741819 完成
Posted
技术标签:
【中文标题】循环向量退出并出现错误进程以退出代码 -1073741819 完成【英文标题】:Loop over vector exits with error Process finished with exit code -1073741819 【发布时间】:2020-07-19 10:33:50 【问题描述】:LE:如果它有任何相关性,我正在使用 CLion。该程序不打印任何内容。
我需要处理一个包含name card_type
形式的字符串向量的向量,并按字母顺序显示所有卡片以及每张卡片的实例数。输出应该类似于:
a_card 1
mastercard 4
visa 3
到目前为止,我的代码以错误Process finished with exit code -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;
【问题讨论】:
您是否使用调试器运行您的代码? 你的迷恋在` for (auto pair = orderedCards.begin(); pair != orderedCards.end(); ++pair) ` 在第二次迭代中,你可以看到你的代码@ 987654321@可以在那里调试 你的核心转储是因为你使用了迭代器,想想你插入一个元素后迭代器会发生什么? here 阅读关于有效性的部分 考虑使用 find 函数或它的一些变体,或者你可以使用 std::map,它的比较器使用字母顺序然后 insert 将按顺序插入 这是你代码的当前输出?bankcard 3 mastercard 1 visa 3
?
【参考方案1】:
@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
【讨论】:
【参考方案2】:感谢@yaodav,我从迭代器切换到地图和排序算法。更新代码和解释:
#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);
【讨论】:
以上是关于循环向量退出并出现错误进程以退出代码 -1073741819 完成的主要内容,如果未能解决你的问题,请参考以下文章
pycharm 随机发生错误:进程以退出代码 134 完成(被信号 6 中断:SIGABRT)
进程以退出代码 139 结束(被信号 11 中断:SIGSEGV)
任何人都知道如何克服 IntelliJ 中的“编译器内部错误。进程以退出代码 138 终止”?