Ternick
@Ternick

Почему при дебаге появляется can't increment vector iterator past end?

#include <vector>
#include <string>
using namespace std;
bool isContain(string str, string searchString) {
	if (str.find(searchString) != string::npos) {
		return TRUE;
	}
	else {
		return FALSE;
	}
}
bool strInVector(vector<string> vec, string str) {
	try{
		for (int i = 0; i < vec.size(); i++) {
			if (isContain(vec[i], str)) {
				return TRUE;
			}
		}
	}
	catch (bad_alloc){
		return FALSE;
	}
	return FALSE;
}
vector<string> split(string str, char symbol) {
	vector<string> pieces;
	string temp = "";
	for (int i = 0; i < str.size(); i++) {
		if (str[i] == symbol) {
			pieces.push_back(temp);
			temp = "";
		}
		else {
			temp += str[i];
		}
	}
	return pieces;
}
int main(int argc, char *argv[]) {
	vector<string> journal;
	while (TRUE) {
		vector<string> data = split(decrypt(getRequest(site, "getNums", getHwid())), ';');
		for (auto i = data.begin(); i != data.end(); i++) {
			if (!strInVector(journal, *i)) {
				//старт процесса и добавление в журнал
				journal.push_back(*i);
				cout << *i << " has been added into journal" << endl;
			}
		}
		for (auto i = journal.begin(); i != journal.end(); i++) {
			if (!strInVector(data, *i)) {
				// удаление из вектора
				cout << *i << " has been removed from journal" << endl;
				journal.erase(i);
			}
		}
		Sleep(5000);
	}
  • Вопрос задан
  • 1543 просмотра
Решения вопроса 1
@lorc
Потому что нельзя изменять вектор, по которому вы итерируете. Вот что пишут про std::vector::erase:

Invalidates iterators and references at or after the point of the erase, including the end() iterator.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы