Questions tagged [erase]

This tag refers to the process of removing or deleting data, text, files, or memory.

739 questions
704
votes
16 answers

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

I have a std::vector, and I want to delete the nth element. How do I do that? Example: std::vector vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); vec.erase(???);
dau_man
  • 7,059
  • 3
  • 17
  • 5
338
votes
4 answers

C++ Erase vector element by value rather than by position?

vector 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…
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
121
votes
7 answers

Erasing elements from a vector

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…
Naveen
  • 74,600
  • 47
  • 176
  • 233
102
votes
15 answers

Erase the current printed console line

How can I erase the current printed console line in C? I am working on a Linux system. For example - printf("hello"); printf("bye"); I want to print bye on the same line in place of hello.
Peter
91
votes
9 answers

How to delete "px" from 245px

Whats a simple way to delete the last two characters of a string?
Tomkay
  • 5,120
  • 21
  • 60
  • 92
84
votes
8 answers

Remove elements of a vector inside the loop

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…
arjacsoh
  • 8,932
  • 28
  • 106
  • 166
70
votes
2 answers

C++ STL map::erase a non-existing key

Regarding the C++ STL map, erasing by key:- size_type map::erase ( const key_type& x ); Is it legal to erase a non-existing key? i.e. is the snippet below ok? map
fuad
  • 4,265
  • 9
  • 34
  • 32
50
votes
5 answers

unlink vs remove in c++

What is the difference between remove and unlink functions in C++?
SyBer
  • 5,407
  • 13
  • 55
  • 64
47
votes
3 answers

Erasing vector::end from vector

Does it works correct(does nothing) when I use vector v; v.erase(v.end()); I want to use something like v.erase(std::find(...)); Should I if is it v.end() or not? There is no info about it on C++.com and CPPreference
RiaD
  • 46,822
  • 11
  • 79
  • 123
46
votes
5 answers

Erase whole array Python

How do I erase a whole array, leaving it with no items? I want to do this so I can store new values in it (a new set of 100 floats) and find the minimum. Right now my program is reading the minimum from sets before I think because it is appending…
pjehyun
  • 911
  • 2
  • 9
  • 11
44
votes
9 answers

HTML how to clear input using javascript?

I have this INPUT, it will clear everytime we click inside of it. The problem: I want to clear only if value = exemplo@exemplo.com
Lucas Ferraz
  • 620
  • 1
  • 7
  • 12
34
votes
1 answer

c++ stl what does base() do

I have such code : vector v; for (int i=0; i<5; i++) v.push_back(i); v.erase(find(v.rbegin(), v.rend(),2).base()); This code deletes the first element from vector v after first detected 2 (in vector remain: 0 1 2 4). What does .base()…
Vitali K
  • 577
  • 1
  • 6
  • 12
32
votes
5 answers

How to erase & delete pointers to objects stored in a vector?

I have a vector that stores pointers to many objects instantiated dynamically, and I'm trying to iterate through the vector and remove certain elements (remove from vector and destroy object), but I'm having trouble. Here's what it looks like: …
Tony R
  • 11,224
  • 23
  • 76
  • 101
24
votes
2 answers

vector::erase(remove(....)) is not working

I came up with a program #include #include using namespace std; int main() { vector a = {1,2,3,7,1,5,4}; vector b = {6,7,4,3,3,1,7}; a.erase(remove(a.begin(),a.end(),a[0]),a.end()); …
Madhu Kumar Dadi
  • 695
  • 8
  • 25
23
votes
4 answers

Erase-remove idiom with std::set failing with constness-related error

Can someone help me out here? Compiling this code: void test() { std::set test; test.insert(42); test.erase(std::remove(test.begin(), test.end(), 30), test.end()); // <- Line 33 } Is generating the following error when compiling: $…
Jared
  • 5,977
  • 10
  • 38
  • 48
1
2 3
49 50