erase

C++ Remove punctuation from String

跟風遠走 提交于 2019-11-26 09:44:46
问题 I got a string and I want to remove all the punctuations from it. How do I do that? I did some research and found that people use the ispunct() function (I tried that), but I cant seem to get it to work in my code. Anyone got any ideas? #include <string> int main() { string text = \"this. is my string. it\'s here.\" if (ispunct(text)) text.erase(); return 0; } 回答1: Using algorithm remove_copy_if :- string text,result; std::remove_copy_if(text.begin(), text.end(), std::back_inserter(result), /

Remove elements of a vector inside the loop

不羁的心 提交于 2019-11-26 07:44:22
I know that there are similar questions to this one, but I didn’t manage to find the way on my code by their aid. I want merely to delete/remove an element of a vector by checking an attribute of this element inside a loop. How can I do that? I tried the following code but I receive the vague message of error: 'operator =' function is unavailable in 'Player’. for (vector<Player>::iterator it = allPlayers.begin(); it != allPlayers.end(); it++) { if(it->getpMoney()<=0) it = allPlayers.erase(it); else ++it; } What should I do? Update: Do you think that the question vector::erase with pointer

How do I erase an element from std::vector<> by index?

北战南征 提交于 2019-11-26 03:24:52
问题 I have a std::vector<int>, and I want to delete the n\'th element. How do I do that? std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); vec.erase(???); 回答1: To delete a single element, you could do: std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); // Deletes the second element (vec[1]) vec.erase(vec.begin() + 1); Or, to delete more than one element at once: // Deletes the second through third elements (vec[1], vec[2]) vec.erase(vec

C++ Erase vector element by value rather than by position? [duplicate]

筅森魡賤 提交于 2019-11-26 02:39:54
问题 This question already has answers here : How do I remove an item from a stl vector with a certain value? (10 answers) Closed 3 years ago . vector<int> myVector; and lets say the values in the vector are this (in this order): 5 9 2 8 0 7 If I wanted to erase the element that contains the value of \"8\", I think I would do this: myVector.erase(myVector.begin()+4); Because that would erase the 4th element. But is there any way to erase an element based off of the value \"8\"? Like: myVector

Remove elements of a vector inside the loop

▼魔方 西西 提交于 2019-11-26 01:58:38
问题 I know that there are similar questions to this one, but I didn’t manage to find the way on my code by their aid. I want merely to delete/remove an element of a vector by checking an attribute of this element inside a loop. How can I do that? I tried the following code but I receive the vague message of error: \'operator =\' function is unavailable in \'Player’. for (vector<Player>::iterator it = allPlayers.begin(); it != allPlayers.end(); it++) { if(it->getpMoney()<=0) it = allPlayers.erase

Erasing elements from a vector

有些话、适合烂在心里 提交于 2019-11-25 22:21:59
问题 I want to clear a element from a vector using the erase method. But the problem here is that the element is not guaranteed to occur only once in the vector. It may be present multiple times and I need to clear all of them. My code is something like this: void erase(std::vector<int>& myNumbers_in, int number_in) { std::vector<int>::iterator iter = myNumbers_in.begin(); std::vector<int>::iterator endIter = myNumbers_in.end(); for(; iter != endIter; ++iter) { if(*iter == number_in) { myNumbers