This question already has answers here:
Erasing elements from a vector
(6 answers)
Closed 8 years ago.
I would like to remove an element from a vector. For example:
// object that is in the vector: MyClass obj;
// vector looks as so: vector<MyClass*> pVector;
pVector.remove(obj);
This will remove the object based purely on the pointer. Ideally, you would have comparison functions for your MyClass objects that actually check the objects to see if they are the same.
pVector.erase(std::remove(pVector.begin(), pVector.end(), obj), pVector.end());
Your question isn't well-defined, but I will provide you with two answers. I am assuming here based on your code that obj is not a pointer, which means that we are comparing an object to pointers to objects. This requires a custom functor.
The first answer is how to remove all elements of the vector where the value of the pointed-to element is equal to obj. This assumes that there is an operator== that can be applied to MyClass objects.
pVector.erase(std::remove_if(pVector.begin(), pVector.end(),
[&obj](MyClass * i) { return i && (*i == obj); }));
The second will remove at most one element, if it is found:
auto e = std::find(pVector.begin(), pVector.end(),
[&obj](MyClass * i) { return i && (*i == obj); });
if (e != pVector.end()) {
pVector.erase(e);
}
The lambda syntax requires C++11. If you don't have access to C++11 then you will have to build a functor by hand:
template <typename T>
class pointer_is_equal_to_object
{
public:
explicit pointer_is_equal_to_object(T const &);
bool operator()(T const *) const;
private:
T const & value;
}
template <typename T>
pointer_is_equal_to_object<T>::pointer_is_equal_to_object(T const & v) : value(v) {}
template <typename T>
bool pointer_is_equal_to_object<T>::operator()(T const * p) const
{
return p && (*p == value);
}
Then, for example, you could use:
pVector.erase(std::remove_if(pVector.begin(), pVector.end(),
pointer_is_equal_to_object<MyClass>(obj)));
Note that this complexity goes away if you stop using pointers and just use std::vector<MyClass>. Then your operator== can be applied directly and you can just do:
pVector.erase(std::remove(pVector.begin(), pVector.end(), obj));
Assuming C++11, and that you want to remove ANY elements matching obj, and not the exact obj... but you should be able to figure it out from here either way :)
http://en.wikipedia.org/wiki/Erase-remove_idiom
And for fun, here's an example: http://ideone.com/6ILYvo
#include <algorithm>
#include <vector>
std::vector<MyClass*> v;
MyClass * toberemoved = new MyClass();
//v gets populated...
auto itr = std::remove_if(v.begin(),v.end(), [&](MyClass* a){return *a == *toberemoved;});
v.erase(itr,v.end());
Related
Say I have a class that has only constants, since the values should never change.
struct Test {
const std::string id;
const int a;
const double b;
};
Later on, I want to add objects into a vector, but I want the vector to be sorted by b from largest to smallest. I use insertion sort, since there will only ever be a small number (maybe 5).
std::vector<Test> values;
void addValue( const std::string &id, int a, double b ) {
// Keep stockpiles sorted by weight (large to small)
auto itr = values.begin();
while ( itr != values.end() && itr->b > b ) ++itr;
values.insert( itr, { id, a, b } );
// end sort
}
Attempting the above, I get the following error while attempting to insert into the vector:
error: object of type 'Test' cannot be assigned because its copy assignment operator is implicitly deleted
I would like to keep with using a vector for this problem; but I can't seem to find a way around the sorting issue. The only other option I could think of was to effectively recreate the vector constantly. Alternatively, using a multi-set or similar, then once all values are added, I could dump into an array.
Is there a way around this limitation, still using a vector and not making everything non-const? Or will I be forced to change my structure, or move away into a temporary object first?
Edit: Also trying to avoid the use of pointers
As has already been pointed out in the comments, sorting requires moving around elements in your vector. And moving around elements generally requires move-assignment, which generally requires mutation of elements…
There is one way out of this dillema: instead of assigning a new value to the existing object, create a new object with the new value on top of the existing one. One can do so by defining a copy/move constructor like, e.g.:
Test& operator =(Test&& other)
{
this->~Test();
return *new (this) Test(std::move(other));
}
There is just one problem with this: via [basic.life]/8.3, we're not allowed to use any existing pointer or reference to the original object, or even just the name of the original object after such an assignment. You would always have to use the result of the assignment (the return value of placement-new) or a laundered pointer as the sole means of accessing the object going forward. Since it is not specified how std::sort operates exactly, we cannoy rely on it doing so.
What we could to is build a wrapper like this (noexcept ommited for readability):
template <typename T>
class const_element
{
T value;
const T& laundered_value() const { return *std::launder(&value); }
T& laundered_value() { return *std::launder(&value); }
public:
template <typename... Args>
explicit const_element(Args&&... args) : value { std::forward<Args>(args)... } {}
const_element(const const_element& e) : value(e.laundered_value()) {}
const_element(const_element&& e) : value(std::move(e.laundered_value())) {}
const_element& operator =(const const_element& e)
{
laundered_value().~T();
new (&value) T(e.laundered_value());
return *this;
}
const_element& operator =(const_element&& e)
{
laundered_value().~T();
new (&value) T(std::move(e.laundered_value()));
return *this;
}
~const_element()
{
laundered_value().~T();
}
operator const T&() const { return laundered_value(); }
operator T&() { return laundered_value(); }
friend bool operator <(const_element& a, const_element& b)
{
return a.laundered_value() < b.laundered_value();
}
};
What this does is wrap an object of some type T, implement copy and move assignment in the way described above, and make sure that any access to the current value always goes through a laundered pointer.
Then we can just do
std::vector<const_element<Test>> values;
values.emplace_back("b", 1, 0.0);
values.emplace_back("a", 0, 0.0);
values.emplace_back("c", 2, 0.0);
std::sort(begin(values), end(values));
working example here
All that being said, I would recommend to just not do this. If you want an object that cannot be modified, simply use a const T rather than a T that solely consists of const members. You cannot have a vector of const T. But you can have a vector of T and then just pass around a reference to a const vector or a range of const elements…
You could store pointers in the vector. Of course, you would also need to clean everything up. Example at http://cpp.sh/3h7dr
std::vector<Test*> values;
void addValue( const std::string &id, int a, double b ) {
// Keep stockpiles sorted by weight (large to small)
auto itr = values.begin();
while ( itr != values.end() && ((*itr)->b > b) ) ++itr;
Test* t = new Test{id,a,b};
values.insert( itr, t );
// end sort
}
I'd like to find a value in unordered_set, but failed:
typedef std::shared_ptr<int> IntPtr;
std::unordered_set<IntPtr> s;
s.insert(std::make_shared<int>(42));
bool found = s.find(std::make_shared<int>(42)) != s.end();
cout<<std::boolalpha<<found<<endl; // false
Had tried following but still not working.
namespace std {
template <> struct hash<IntPtr> {
size_t operator()(const IntPtr& x) const noexcept {
return std::hash<int>()(*x);
}
};
}
Any idea how to make it works?
You stored a pointer to an integer. When you look up items in the set, you're not comparing the (pointed-to) integer, but the pointer itself.
When you allocate a new pointer to a new integer object for the search, it won't compare equal, because it's a different integer object (even though it stores the same value).
Your options are:
don't store pointers to integers in your set, just store the integers directly.
Then, your key is 42, and searching for 42 will find it, because the integers are compared by value
store pointers and use a custom hash and comparator to compare the pointed-at integers instead of the pointers.
You shouldn't (try to) pollute std namespace with your hash specialization, and it's not sufficient anyway (the hash is used for bucket lookup, but keys are still compared with KeyEqual inside the bucket). Just specify them for your container.
Example code for #2:
#include <cassert>
#include <memory>
#include <unordered_set>
struct Deref {
struct Hash {
template <typename T>
std::size_t operator() (std::shared_ptr<T> const &p) const {
return std::hash<T>()(*p);
}
};
struct Compare {
template <typename T>
size_t operator() (std::shared_ptr<T> const &a,
std::shared_ptr<T> const &b) const {
return *a == *b;
}
};
};
int main() {
std::unordered_set<std::shared_ptr<int>> sp;
auto p = std::make_shared<int>(42);
sp.insert(p);
assert(sp.find(p) != sp.end()); // same pointer works
assert(sp.find(std::make_shared<int>(42)) == sp.end()); // same value doesn't
// with the correct hash & key comparison, both work
std::unordered_set<std::shared_ptr<int>, Deref::Hash, Deref::Compare> spd;
spd.insert(p);
assert(spd.find(p) != spd.end());
assert(spd.find(std::make_shared<int>(42)) != spd.end());
}
According to here:
Note that the comparison operators for shared_ptr simply compare pointer values; the actual objects pointed to are not compared.
So found will be true only if shared_ptr points to same object:
typedef std::shared_ptr<int> IntPtr;
std::unordered_set<IntPtr> s;
IntPtr p = std::make_shared<int>(42);
s.insert(p);
bool found = s.find(p) != s.end();
cout<<std::boolalpha<<found<<endl; // true
I have the following struct
struct MyClass {
int myInt;
std::map<int, int> myMap;
};
I want to use unordered_set<MyClass*, PointedObjHash, PointedObEq> but I can't find a valid way to declare PointedObEq.
I tried
struct PointedObjHash {
size_t operator() (MyClass* const& c) const {
std::size_t seed = 0;
boost::hash_combine(seed, c->myInt);
boost::hash_combine(seed, c->myMap);
return seed;
}
and I hope it is fine, but I can't find a way to declare PointedObjEq
--- EDIT ---
If declare operator== inside the class debug never breaks, but I think 'cause MyClass == MyClass* never happens...
struct MyClass {
...
...
bool operator==(MyClass* const& c) {
return this->myInt == c->myInt & this->myMap == c->myMap;
}
If declare operator== inside the class debug never breaks, but I think 'cause MyClass == MyClass* never happens...
The unordered_set needs to use operator== (or PointedObjEq) to double-check the results of the hash function. The hash provides approximate equality, the equality function is used to weed out false positives.
If you've tested adding the same value to the set twice, then you've tested the equality function. To be sure, of course, you can have it print something to the console.
Since it's impossible to define an operator== function with two pointer operands, the PointedObjEq class will be necessary. Note that it takes a MyClass const * on both sides. Also, there's no need to use a reference to a pointer.
So,
struct PointedObjEq {
bool operator () ( MyClass const * lhs, MyClass const * rhs ) const {
return lhs->myInt == rhs->myInt
&& lhs->myMap == rhs->myMap;
}
};
This should do:
struct PointedObEq {
bool operator()(MyClass const * lhs, MyClass const * rhs) const {
return lhs->myInt == rhs->myInt && lhs->myMap == rhs->myMap;
}
};
The reason why your solution does not work is because you have effectively written a mechanism to compare a MyClass with a MyClass*, when you actually need something to compare a MyClass* with a MyClass*.
P.S.: My original answer passed the pointers by const&. Thinking about it, that's a strange coding style, so I changed it to pass the pointers by value.
typedef MyClass* PtrMyClass;
struct PointedObjCompare
{ // functor for operator==
bool operator()(const PtrMyClass& lhs, const PtrMyClass& rhs) const
{
// your code goes here
}
};
std::unordered_set < MyClass*, PointedObjHash, PointedObjCompare > myset;
I want to implement a sorted pointer vector, like the following
#include <vector>
#include <memory>
#include <algorithm>
//! A random accessed vector with sorted allocated elements.
//! - Elements must be allocated on heap.
//! - The vector manages the memories of its elements.
template<class T, class Compare = std::less<T>>
class SortedPtrVector
{
public:
SortedPtrVector() {}
//! Add an element, return its index.
int Add(T* element)
{
auto position = std::lower_bound(m_vector.begin(), m_vector.end(),
element, Compare); // Wrong here due to compare smart pointers
auto newPosition = m_vector.insert(position, element);
return newPosition - m_vector.begin();
}
private:
std::vector<std::unique_ptr<T>> m_vector;
};
How to implement the Add function? Thanks a lot.
auto position = std::lower_bound(m_vector.begin(), m_vector.end(),
element, Compare);
This is obviously wrong. Compare is a type, not an object.
You could use lambda with an object of Compare. So I think this should work:
Compare cmp;
auto comparer = [&](std::unique_ptr<T> const & a, std::unique_ptr<T> const & b)
{
return cmp(*a, *b); //use cmp here!
};
std::unique_ptr<T> uniqElem(element);
auto position = std::lower_bound( m_vector.begin(),
m_vector.end(),
uniqElem, //not element!!
comparer);
Note that you cannot pass element to std::lower_bound, as element is of type T*, when the std::lower_bound expects the value of type std::unique_ptr<T> and there is no implicit conversion from T* to std::unique_ptr<T>. Also, you cannot insert element to the vector for the same reason. Insert uniqElem to the vector.
I would suggest you to take the argument as unique_ptr instead of T*, because that indicates to the user that the added item will be deleted automatically when an object of SortedPtrVector goes out of scope:
int Add(T* element); //bad - doesn't say element will be deleted!
int Add(std::unique_ptr<T> element); //good - says element will be deleted!
If you use std::unique_ptr<T> as parameter type, then note these points:
v.Add(new T()); //will not work
v.Add(std::unique_ptr<T>(new T()); //will work
std::unique_ptr<T> item(new T());
v.Add(item); //will not work
v.Add(std::move(item)); //will work
It is all because std::unique_ptr is NOT copyable, but it is moveable.
Instead of using std::less you can implement your own ptr_less like this:
template< typename T >
class ptr_less
{
typedef bool result_type;
bool operator ()( T const& left, T const& right ) const
{
return *left < *right;
}
};
A general implementation would have to check for null pointers as well.
Another approach would be to use boost::ptr_vector instead of std::vector.
I want to find in a vector of Object pointers for a matching object. Here's a sample code to illustrate my problem:
class A {
public:
A(string a):_a(a) {}
bool operator==(const A& p) {
return p._a == _a;
}
private:
string _a;
};
vector<A*> va;
va.push_back(new A("one"));
va.push_back(new A("two"));
va.push_back(new A("three"));
find(va.begin(), va.end(), new A("two"));
I want to find the second item pushed into the vector. But since vector is defined as a pointers collection, C++ does not use my overloaded operator, but uses implicit pointer comparison. What is the preferred C++-way of solutiono in this situation?
Use find_if with a functor:
template <typename T>
struct pointer_values_equal
{
const T* to_find;
bool operator()(const T* other) const
{
return *to_find == *other;
}
};
// usage:
void test(const vector<A*>& va)
{
A* to_find = new A("two");
pointer_values_equal<A> eq = { to_find };
find_if(va.begin(), va.end(), eq);
// don't forget to delete A!
}
Note: your operator== for A ought to be const, or, better still, write it as a non-member friend function.
Either use std::find_if and provide a suitable predicate yourself, see other answers for an example of this.
Or as an alternative have a look at boost::ptr_vector, which provides transparent reference access to elements which are really stored as pointers (as an extra bonus, memory management is handled for you as well)
Try using find_if instead. It has a parameter for a predicate where you can decide exactly how to check wheter you found the right element.
http://www.sgi.com/tech/stl/find_if.html
You could also use Boost::Lambda:
using namespace boost::lambda;
find_if(va.begin(), va.end(), *_1 == A("two"));
Of course, you should prefer to use shared_ptrs so you don't have to remember to delete!