Click here to Skip to main content
15,946,074 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Please - explain the C++ code / function Pin
Richard MacCutchan28-Dec-23 22:02
mveRichard MacCutchan28-Dec-23 22:02 
AnswerRe: Please - explain the C++ code / function Pin
jschell29-Dec-23 5:16
jschell29-Dec-23 5:16 
AnswerRe: Please - explain the C++ code / function Pin
jeron112-Jan-24 5:04
jeron112-Jan-24 5:04 
GeneralRe: Please - explain the C++ code / function Pin
Richard MacCutchan12-Jan-24 5:39
mveRichard MacCutchan12-Jan-24 5:39 
GeneralRe: Please - explain the C++ code / function Pin
jeron112-Jan-24 6:16
jeron112-Jan-24 6:16 
Questiondeleting from a vector Pin
mike741124-Dec-23 20:37
mike741124-Dec-23 20:37 
AnswerRe: deleting from a vector Pin
Richard MacCutchan24-Dec-23 21:39
mveRichard MacCutchan24-Dec-23 21:39 
AnswerRe: deleting from a vector Pin
Mircea Neacsu25-Dec-23 2:19
Mircea Neacsu25-Dec-23 2:19 
As you guessed, erase invalidates all iterators on the container. Your problem is known as the Erase–remove idiom[^] There are different ways of solving it:
1. The "manual" way:
C++
auto pos = myVector.begin();
size_t i=0;
while (pos != myVector.end())
{
  if (*pos ==3)
  {
    myVector.erase(pos);
    pos = myVector.begin() + i;
  }
  else
  {
    ++i;
    ++pos;
  }
}

2. The "semi-automatic" way:
C++
auto pos = myVector.begin();
while (pos != myVector.end())
{
  if (*pos ==3)
    pos = myVector.erase(pos); //erase returns an iterator pointing to next element
  else
    ++pos;
}

3. The "old fully automatic way":
C++
myVector.erase(std::remove(myVector.begin(), 
                              myVector.end(), 3),
               myVector.end());

4. The "new fully automatic way" (since C++20):
C++
std::erase (myVector, 3);

Disclaimer: I didn't compile any of the code above; some errors may/will exist Smile | :)
Mircea

Questionstd::async Pin
utcode23-Dec-23 16:31
utcode23-Dec-23 16:31 
AnswerRe: std::async Pin
Richard MacCutchan23-Dec-23 21:32
mveRichard MacCutchan23-Dec-23 21:32 
Questiondesign question : variadic functions use ? . Pin
Maximilien19-Dec-23 2:34
Maximilien19-Dec-23 2:34 
AnswerRe: design question : variadic functions use ? . Pin
Mircea Neacsu19-Dec-23 2:42
Mircea Neacsu19-Dec-23 2:42 
GeneralRe: design question : variadic functions use ? . Pin
Maximilien19-Dec-23 2:54
Maximilien19-Dec-23 2:54 
AnswerRe: design question : variadic functions use ? . Pin
jschell19-Dec-23 5:08
jschell19-Dec-23 5:08 
GeneralRe: design question : variadic functions use ? . Pin
Maximilien19-Dec-23 7:36
Maximilien19-Dec-23 7:36 
GeneralRe: design question : variadic functions use ? . Pin
jschell20-Dec-23 5:31
jschell20-Dec-23 5:31 
GeneralRe: design question : variadic functions use ? . Pin
Maximilien20-Dec-23 6:55
Maximilien20-Dec-23 6:55 
GeneralRe: design question : variadic functions use ? . Pin
jschell21-Dec-23 3:31
jschell21-Dec-23 3:31 
Questionstrcpy_s weirdness Pin
mike741119-Dec-23 1:31
mike741119-Dec-23 1:31 
AnswerRe: strcpy_s weirdness Pin
Mircea Neacsu19-Dec-23 1:43
Mircea Neacsu19-Dec-23 1:43 
AnswerRe: strcpy_s weirdness Pin
Richard MacCutchan19-Dec-23 2:04
mveRichard MacCutchan19-Dec-23 2:04 
AnswerRe: strcpy_s weirdness Pin
CPallini19-Dec-23 2:09
mveCPallini19-Dec-23 2:09 
QuestionShould this work in C++? Pin
mike741118-Dec-23 7:07
mike741118-Dec-23 7:07 
AnswerRe: Should this work in C++? Pin
Mircea Neacsu18-Dec-23 7:17
Mircea Neacsu18-Dec-23 7:17 
AnswerRe: Should this work in C++? Pin
Maximilien19-Dec-23 5:11
Maximilien19-Dec-23 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.