为什么擦除向量元素会导致循环崩溃? [英] Why erasing a vector element crashes the loop?

查看:48
本文介绍了为什么擦除向量元素会导致循环崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前提出的问题的延续名人堂成员 [< a href =http://www.codeproject.com/Questions/1079742/What-c​​ontainer-should-i-choosetarget =_ blanktitle =New Window> ^ ]

遵循 Richard MacCutchan 的建议[ ^ ],我在Hall Of的实现中尝试使用vector名声和分数的名气。

名称不应该有重复,分数应该排序。

但是在这里我被卡住了。

This is a continuation from my previous question Hall of Fame[^]
By following the advice from Richard MacCutchan[^], I tried using vector in my implementation of Hall Of Fame which has name and score.
The name should not have duplicate and score should be sorted.
However here I'm stuck.

bool myfunction(std::pair<string, unsigned int> rhs, std::pair<string,unsigned int> lhs) {
	return ( rhs.second > lhs.second );
}

std::vector < std::pair<string, unsigned int>> hall;
hall.push_back(std::make_pair("One", 50));
hall.push_back(std::make_pair("Two", 30));
hall.push_back(std::make_pair("Three", 10));
hall.push_back(std::make_pair("Two", 30));
hall.push_back(std::make_pair("Three", 20));
std::sort(hall.begin(), hall.end(), myfunction);
for ( auto c : hall ) {
	cout << c.first << " " << c.second << endl;
}
for ( auto a : hall ) {
	for ( auto b : hall ) {
		if ( ( a.first == b.first ) && ( a.second != b.second ) ) {
			if ( a.second > b.second ) {
				hall.erase(b);//???
			}
		}
	}
}



我将矢量复制到一个集合中<对><串,无符号>>它解决了重复输入的问题(两个,30)。



如何从向量中移除对(Three,10)。

无论我知道什么循环/迭代器技巧不工作。

在其中一个尝试版本中(使用迭代器)我设法擦除了对(三,10),但它在下一步(使用调试器)失败,因为(我认为)迭代器指向下一次迭代中的无效位置或更改了矢量大小。



我是编程的新手

我真的觉得我错过了这里很简单。

我该怎么办?请教我一点。



我尝试过:



在对矢量进行排序之后

- 我尝试使用指向b的迭代器进行擦除。

- 我尝试使用另一个循环(int i = 0; i< hall.size ();> -unsuccessful使用std :: find搜索具有相同字符串但值不同的对(因此嵌套a和b for循环)。

-reading cplusplus.com关于它


I've copy the vector into a set<pair><string,unsigned>> and it solve problem with duplicated entry ("Two",30).

How can I remove the pair("Three",10) from the vector.
Whatever loop / iterator trick that I know do not work.
In one of tries versions (using iterator) I managed to erase the pair("Three",10) but it failed in the next step (using debugger) because (I think) the iterator pointing to invalid location in the next iteration or vector size changed.

I'm a newbie to programming
I really think I missed something simple here.
What should I do. Please teach me a bit.

What I have tried:

after sorting the vector
-I've tried erasing using iterator pointing b.
-I've tried using another loop (int i=0;i<hall.size();>-unsuccessful using std::find to search for pairs that have same string but different value (hence the nested a and b for loop).
-reading cplusplus.com about it

推荐答案

删除重复项很棘手,因为你可以动态改变矢量内容。

范围循环优雅但不适合例如,尝试



removing duplicates is tricky, because you alter the vector content on-the-fly.
The range loop is elegant but simply not suited for such a job. Try, for instance

auto itm = hall.begin();

while(itm != hall.end())
{

  auto itn = hall.end();
  while(--itn != itm)
  {
    if ( itm->first == itn->first && itm->second == itn->second )
      hall.erase(itn);
  }
  ++itm;
}





优化排序向量的代码留作练习。



Optimize the code for the sorted vector is left as exercise.


这篇关于为什么擦除向量元素会导致循环崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆