|
-
May 19th, 2011, 12:33 AM
#12
Re: remove multiple elements from vector in a loop
Thanks for all the replies. Sorry for the delay, I got invited to a friends house for dinner.
 Originally Posted by kempofighter
Did you look at the example? You could have copy and pasted the example from that link and compiled it. Simply substitute your function names.
The example had the data in an array instead of a vector. I couldn't tell if the function wanted pointers or iterators, or pointers to iterators, etc, and the things I tried (myVector.begin(), etc) wouldn't compile.
 Originally Posted by Paul McKenzie
Code:
#include <algorithm>
#include <vector>
#include <iostream>
bool RemoveMe(int val)
{
return ( val == 3 || val == 4);
}
int main()
{
std::vector<int> vect(3);
vect[0] = 3;
vect[1] = 4;
vect[2] = 5;
vect.erase(std::remove_if(vect.begin(), vect.end(), RemoveMe), vect.end());
for (size_t i = 0; i < vect.size(); ++i )
std::cout << vect[i] << "\n";
}
Is this what you're trying to code?
But in general, if you're writing convoluted loops to remove elements from a container in C++, then you need to take a step back and rethink what you're doing, as more than likely, an alogrithm or set of algorithm functions will do the job, safely and correctly.
This is more or less it, but I already know that I want to remove "val" before I pass it to the remove function. I tried several things like this, but I think I was misunderstanding what the function was returning. I was trying things like,
Code:
bool RemoveMe(int val) {
return val;
}
because I thought the function was returning the value of what was to be removed, not weather or not to remove the value that was passed. I also, of course, was confused about why/how the function could be returning a bool in that context. The RemoveMe(int val) function,
Code:
bool RemoveMe(int val)
{
return ( val == 3 || val == 4);
}
returns true if the passed value == 3 or 4? I guess I was not seeing the parentheses with the return. It may make more sense to evaluate remove/keep in a function like this instead of how I have it now. I will look at that more carefully now that I understand how this works. As usual, I revert to loops because that is what I know how to do. The loop I got working is pretty simple, especially for small structures like this, but I don't learn anything if I just stick to what I already know.
 Originally Posted by Lindley
You just have to get used to the terminology. All me to translate:
"Unary predicate (function taking one argument) taking an element in the range as argument (the function's argument type must match the type in the container), and returning a value indicating the falsehood (with false, or a zero value) or truth (true, or non-zero) of some condition applied to it (the function must return a bool indicating whether or not to remove the element in question). This can either be a pointer to a function or an object whose class overloads operator() (You can specify the function several different ways.)"
Such formal language can have a learning curve, but its preciseness is useful when you want to know exactly what a container or algorithm is going to do.
This is what I wish all the documentation looked like, so thanks for the clarification. With just the added phrases above, I can completely understand the previous posts, which I spent half the afternoon trying to implement with no luck (mostly because I didn't under stand what the function was expecting and returning). I do get frustrated at times with the C++ doc. I have three degrees, two in hard sciences, and this isn't my first day programming, but it still takes forever for me to get anywhere with some of this stuff. My non-science degree is in tech writing, so I get that things are written to a specific audience, but it seems like a little bit of plain language , like in Lindley's post, wouldn't exactly stop the world from turning on its axis. Sometimes I feel like I don't understand because I don't know the secret handshake and that you have to look at it upside down in a mirror. Mostly I get frustrated with myself about how long it takes to do things when I know what I want to do, but not exactly how to implement it. I don't expect to be an expert at something I'm, well, not an expert at. I presume that everyone here worked pretty hard to acquire the skills and knowledge they possess. I don't mind putting in the time, but some days it would be nice it something would actually just work, without having to wrestle with it all afternoon.
Chemistry, Biology, and AI certainly have plenty of specific terminology, so I won't complain too much, plus that's a bore to listen to, even where it's justified. I do think there are probably a few more people who are interested in deciphering c++ code than calculating ab inito molecular orbitals from the Schrödinger Equation, so broadening the audience just a bit couldn't hurt.
Thanks again, I will try again later toady after a bit of shut eye.
LMHmedchem
Last edited by LMHmedchem; May 19th, 2011 at 12:44 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|