473,588 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

remove elements from vector

Hello,

I am thinking an efficient way to remove all the elements (says they
are less than 10) from a container vector<int>.

Any suggestion? Thank you.

Oct 31 '06 #1
5 2646
Wing wrote:
Hello,

I am thinking an efficient way to remove all the elements (says they
are less than 10) from a container vector<int>.

Any suggestion? Thank you.
Look into std::remove_if, std::less, std::bind2nd, and the erase-remove
idiom.

Nate
Oct 31 '06 #2
Wing wrote:

I am thinking an efficient way to remove all the elements (says they
are less than 10) from a container vector<int>.

Any suggestion? Thank you.
Any reason not to use std::vector::cl ear()?

Best regards,

Tom

Oct 31 '06 #3

Nate Barney wrote:
Wing wrote:
Hello,

I am thinking an efficient way to remove all the elements (says they
are less than 10) from a container vector<int>.

Any suggestion? Thank you.

Look into std::remove_if, std::less, std::bind2nd, and the erase-remove
idiom.

Sounds like overkill if the OP wants to remove _all_ the elements.

Best regards,

Tom

Oct 31 '06 #4
Thomas Tutone wrote:
Nate Barney wrote:
>Wing wrote:
>>Hello,

I am thinking an efficient way to remove all the elements (says they
are less than 10) from a container vector<int>.

Any suggestion? Thank you.
Look into std::remove_if, std::less, std::bind2nd, and the erase-remove
idiom.

Sounds like overkill if the OP wants to remove _all_ the elements.
I first read the question as "How does one remove all elements that are
less than 10?". I suppose it could be read as "How does one remove all
elements of a vector that has fewer than 10 elements?". Well, now the
OP has an answer either way.

Nate
Oct 31 '06 #5
Nate Barney wrote:
Thomas Tutone wrote:
Nate Barney wrote:
Wing wrote:
Hello,

I am thinking an efficient way to remove all the elements (says they
are less than 10) from a container vector<int>.

Any suggestion? Thank you.
Look into std::remove_if, std::less, std::bind2nd, and the erase-remove
idiom.
Sounds like overkill if the OP wants to remove _all_ the elements.

I first read the question as "How does one remove all elements that are
less than 10?". I suppose it could be read as "How does one remove all
elements of a vector that has fewer than 10 elements?". Well, now the
OP has an answer either way.
Now I understand. I hadn't read the OP's question that way, but after
reading your interpretation I went back and reread the OP's question,
and now I see your point.

Best regards,

Tom

Oct 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2194
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in the process), I looked up the STL code. Erase certainly does not delete the memory associated with the element. However, it appears that the destructor on the element is invoked. I wonder why it has to be this way. In my opinion, this renders...
5
2556
by: Yngve | last post by:
Hi! I have a (newbie) problem wich i would become glad if someone could help me with. I have a Vector of pointers to instances of another class. I would like to remove a position in the middle of the vector. I am not using iterators at the moment, but if i had i think that i could have used "erase". This is my code now:
34
4149
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
11
2731
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
6
4934
by: Arne Claus | last post by:
Hi If've just read, that remove() on a list does not actually remove the elements, but places them at the end of the list (according to TC++STL by Josuttis). It also says, that remove returns a new, logical end pointer, so that the following myList::iterator end = myListObj.remove(myInt); myListObj.erase(end, myListObj.end()); is possible and removes the "invalid" items at the end of the list.
6
3398
by: happyvalley | last post by:
Hi, I want to remove some elements from a vector, the following code doesn't work, seems it doesn't allow me to remove an element when iterating the vector. (make sense), just wonder, how to do this? thank vector<intIntVec; vector<int>::iterator intIterator; for(int i=0; i<10;i++) IntVec.push_back(i);
3
2852
by: groups | last post by:
This small piece of code is troubling me.. is there anything wrong with it?? After calling this method the contents ot the input vector are completely screwed.. (CCountedCIG_CountZero() applies only to a few elements in the vector) void UElementVector::ClearZeroCountElements(vector<CCountedCIG*> &ioItemVec) { vector<CCountedCIG*>::iterator new_end = remove_if(ioItemVec.begin(),
7
1655
by: mohammaditraders | last post by:
Write a program which overloads a binary Minus (+) operator, The program will contain a class Matrix, This class will contain a private data member Array which store int values. The class will further contain a Default constructor, get() function which takes values for array from the user and also contain a Display function witch display the array on the screen, In main function create three objects Mat1, Mat2, Mat3 of this class, first...
2
2374
by: Angus | last post by:
Hello I have a vector<int(aRemovecoll) which is a list of the indexes to be removed from another vector. The other vecotr contains an object - I will call it SomeObject. So the other vecotr is a vector<SomeObject>. I have created a function like this so far: UINT uNumElements = aRemoveColl.GetSize();
0
8222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6634
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5726
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2371
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1458
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1195
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.