debug assert vector iterator not dereferncable with find_if - c++

I want to convert my for loop into a find_if lambda but I Always get the same result vector iterator not dereferncable.
void SearchObjectDescription(std::vector<std::string>& input, Player & player)
{
//--local variable
bool found = false;
//std::find_if
std::vector<std::string>::iterator i = std::find_if(input.begin(), input.end(),[&](Player player)
{
if ((player.InInventory((*i)) ) == true)
{
std::cout << (player.GetInventory().ItemByName((*i))).ExamineObject() << std::endl;
return true;
}
else
{
std::cout << "Object not in invetory!" << std::endl;
return false;
}
});
//original for loop
//for (std::vector<std::string>::iterator i = input.begin()+1; i != input.end(); i++)
//{
// if (player.InInventory((*i))== true)
// {
// std::cout << (player.GetInventory().ItemByName((*i))).ExamineObject() << std::endl;
// found = true;
// break;
// }
//}
//if (found ==false)
//{
// std::cout << "Object not in invetory!" << std::endl;
//}
}
Can some one help me please?

You are thinking about the lambda wrongly. The std::find_if function takes, as it's third argument, a lambda that takes an element of the vector, and return if it's the element you searched for.
The purpose of the lambda is to take an element and tell if it's the right one.
Yet, your lambda don't receive an element of your vector of string, but takes a player as parameter. But you obviously don't have a list of player, you have a list of string. Why should the element to check should be a Player?
Instead, capture your player variable and receive the element to check. It would look like this:
void SearchObjectDescription(std::vector<std::string>& input, Player & player)
{
auto i = std::find_if(input.begin(), input.end(),[&](const std::string& item)
{
// item is equal to the current element being checked
// player is captured by reference because of [&]
// if the item `item` is in inventory, return true.
return player.InInventory(item);
});
if (i != input.end()) {
// if it's not equal to the end, *i is the found item.
}
}
Note that in C++14, you can receive auto&& in your lambda, and it will be deduced to string&:
std::find_if(input.begin(), input.end(), [&](auto&& item)
{
// type of item is std::string&
// ...
});

You cannot use the i iterator inside of your lambda, because it is not initialized until after std::find_if() exits. You need to use the input parameter of the lambda instead, which will be a std::string from the vector, not a Player object.
Also, you are not checking the return value of std::find_if() to make sure you have a valid iterator before dereferencing it.
You did not translate your for loop to a lambda-based std::find_if() correctly. Try this instead:
void SearchObjectDescription(std::vector<std::string>& input, Player & player)
{
auto i = std::find_if(input.begin()+1, input.end(),
[&](const std::string &s) { return player.InInventory(s); });
if (i != input.end())
{
std::cout << (player.GetInventory().ItemByName(*i)).ExamineObject() << std::endl;
}
else
{
std::cout << "Object not in inventory!" << std::endl;
}
}

Related

Range-based for loop with special case for the first item

I find myself often with code that looks like this:
bool isFirst = true;
for(const auto &item: items)
{
if(!isFirst)
{
// Do something
}
// Normal processing
isFirst = false;
}
It seems that there ought to be a better way to express this, since it's a common pattern in functions that act like a "join".
Maybe a for_first_then_each is what you're looking for? It takes your range in terms of iterators and applies the first function to the first element and the second function to the rest.
#include <iostream>
#include <vector>
template<typename BeginIt, typename EndIt, typename FirstFun, typename OthersFun>
void for_first_then_each(BeginIt begin, EndIt end, FirstFun firstFun, OthersFun othersFun) {
if(begin == end) return;
firstFun(*begin);
for(auto it = std::next(begin); it != end; ++it) {
othersFun(*it);
};
}
int main() {
std::vector<int> v = {0, 1, 2, 3};
for_first_then_each(v.begin(), v.end(),
[](auto first) { std::cout << first + 42 << '\n'; },
[](auto other) { std::cout << other - 42 << '\n'; }
);
// Outputs 42, -41, -40, -39
return 0;
}
You can't know which element you are visiting in a range based for loop unless you are looping over a container like an array or vector where you can take the address of the object and compare it to the address of the first item to figure out where in the container you are. You can also do this if the container provides lookup by value, you can see if the iterator returned from the find operation is the same as the begin iterator.
If you need special handling for the first element then you can fall back to a traditional for loop like
for (auto it = std::begin(items), first = it, end = std::end(items); it != end; ++it)
{
if (it == first)
{
// do something
}
// Normal processing
}
If what you need to do can be factored out of the loop then you could use a range based for loop and just put the processing before the loop like
// do something
for(const auto &item: items)
{
// Normal processing
}
With Ranges coming in C++20, you can split this in two loops:
for (auto const& item : items | view::take(1)) {
// first element only (or never executed if items is empty)
}
for (auto const& item : items | view::drop(1)) {
// all after the first (or never executed if items has 1 item or fewer)
}
If you don't want to wait for C++20, check out range-v3 which supports both of these operations.
This won't work like this with an Input range (like if items is really a range that reads from cin) but will work just fine with any range that is Forward or better (I'm guessing items is a container here, so that should be fine).
A more straightforward version is actually to use enumerate (which only exists in range-v3, not in C++20):
for (auto const& [idx, item] : view::enumerate(items)) {
if (idx == 0) {
// first element only
}
// all elements
}
A fun alternative solution, that I would not use in production without great care, would be to use custom iterator.
int main() {
std::vector<int> v{1,2,3,4};
for (const auto & [is_first,b] : wrap(v)) {
if (is_first) {
std::cout << "First: ";
}
std::cout << b << std::endl;
}
}
A toy implementation could look like this:
template<typename T>
struct collection_wrap {
collection_wrap(T &c): c_(c) {}
struct magic_iterator {
bool is_first = false;
typename T::iterator itr;
auto operator*() {
return std::make_tuple(is_first, *itr);
}
magic_iterator operator++() {
magic_iterator self = *this;
itr++;
//only works for forward
is_first = false;
return self;
}
bool operator!=(const magic_iterator &o) {
return itr != o.itr;
}
};
magic_iterator begin() {
magic_iterator itr;
itr.is_first = true;
itr.itr = c_.begin();
return itr;
}
magic_iterator end() {
magic_iterator itr;
itr.is_first = false;
itr.itr = c_.end();
return itr;
}
T &c_;
};
template<typename Collection>
collection_wrap<Collection>
wrap(Collection &vec) {
return collection_wrap(vec);
}
Check the object address to see if it's the first item:
for(const auto &item: items)
{
if (&item != &(*items.begin())
{
// do something for all but the first
}
// Normal processing
}
An approach still valid in C++ is to use a macro:
#include <iostream>
#include <vector>
#define FOR(index, element, collection, body) { \
auto &&col = collection; \
typeof(col.size()) index = 0; \
for(auto it=col.begin(); it!=col.end(); index++, it++) { \
const auto &element = *it; \
body; \
} \
}
using namespace std;
int main() {
vector<int> a{0, 1, 2, 3};
FOR(i, e, a, {
if(i) cout << ", ";
cout << e;
})
cout << endl;
FOR(i, e, vector<int>({0, 1, 2, 3}), {
if(i) cout << ", ";
cout << e;
})
cout << endl;
return 0;
}
Prints:
0, 1, 2, 3
0, 1, 2, 3
This solution is succinct compared to alternative options. On the downside, index is being tested and incremented on each iteration of the loop - this can be avoided by increasing the complexity of the macro and by using bool first instead of index, but using index in the macro covers more use cases than bool first.
Since C++20, you can slightly improve your range-based for loop by using an init-statement. The init-statement allows you to move your isFirst flag into the scope of the loop so that this flag is no longer visible outside the loop:
std::vector<int> items { 1, 2, 3 };
for(bool isFirst(true); const auto &item: items) {
if(!isFirst) {
std::cout << "Do something with: " << item << std::endl;
}
std::cout << "Normal processing: " << item << std::endl;
isFirst = false;
}
Output:
Normal processing: 1
Do something with: 2
Normal processing: 2
Do something with: 3
Normal processing: 3
Code on Wandbox
I assume you want to know how to retrieve the first element, you could do this with an array and a vector.
I'm going to show the array here.
First include this in your code:
#include <array>
Then convert your array accordingly:
std::array<std::string, 4> items={"test1", "test2", "test3", "test4"};
for(const auto &item: items)
{
if(item == items.front()){
// do something
printf("\nFirst: %s\n", item.c_str()); //or simply printf("\nFirst:"); since you gonna output a double element
}
// Normal processing
printf("Item: %s\n", item.c_str());
}
return 0;
}

Differences in erasing an entity from a list in C++

I am trying to erase an entity from a list in two cases. In the first case the following worked just fine, where I have a list of pairs from which I want to erase a certain one:
bool remove(std::list<std::pair<std::string, size_t>>& myList, std::string const& name) {
for (auto i = myList.begin(); i != myList.end(); i++) {
if(i->first == name) {
myList.erase(i);
return true;
}
}
return false;
}
Here the pair gets removed from the list as it should, but when I have a list of structs it does not work as in the following:
void remove(std::list<myStruct>& myList , const std::string& name) {
for (auto i = myList.begin(); i != myList.end(); i++) {
if(i->name == name) {
myList.erase(i);
}
}
The program crashes in the erase part. Only if I plug in myList.erase(i++) then it works. Why is this??
Have I done something foul in the first case and it just happened to work, but then in the second case it does not? I can not understand the reason.
You're working on an invalidated iterator. That's undefined behavior. That's why erase returns a valid iterator.
If you want to only erase the first matching element, use find_if and then erase if the returned iterator isn't equal to end().
auto it = find_if(myList.begin(), myList.end(), [&name](auto const& p){
return p.first == name;
});
if(it == myList.end()){
return false;
}
myList.erase(it);
return true;
Otherwise, just use erase-remove idiom and be wary for its pitfalls (erase will happily accept 1 argument, but it'll call a different overload):
auto it = remove_if(myList.begin(), myList.end(), [&name](auto const& p){
return p.first == name;
});
myList.erase(it, myList.end());
The above is a generic version (will work if you change myList's type to vector for example), but as per ksfone's reply, std::list<T> implements member function template remove_if:
myList.remove_if([&name](auto const& p){
return p.first == name;
});
Your first loop removes an entry from the list and stops.
for (auto i = myList.begin(); i != myList.end(); i++) {
if(i->first == name) {
myList.erase(i);
return true;
}
}
while your second loop continues looking for matching elements:
for (auto i = myList.begin(); i != myList.end(); i++) {
if(i->name == name) {
myList.erase(i);
}
}
When you erase i from myList, i becomes invalid - we have no idea what it references now that the element it was talking about has gone away and may have been deleted, returned to the os and it's memory used by another thread.
The very next thing you do is i++ which is undefined behavior, since i is an invalid iterator.
The erase operator returns the next valid iterator, so you could write this:
for (auto i = myList.begin(); i != myList.end();) {
if(i->name == name)
i = myList.erase(i);
else
i++;
}
Or you could write:
void remove(std::list<myStruct>& myList , const std::string& name) {
myList.remove_if([&](const myStruct& it) { return it.name == name; });
}
or if your compiler supports C++14
void remove(std::list<myStruct>& myList , const std::string& name) {
myList.remove_if([&](auto& it) { return it.name == name; });
}
As in
#include <iostream>
#include <list>
#include <string>
struct A {
std::string name_;
A(const char* name) : name_(name) {}
};
void dump(const std::list<A>& list) {
for (auto&& a : list) {
std::cout << a.name_ << ' ';
}
std::cout << '\n';
}
int main() {
std::list<A> myList { { "hello", "pizza", "world", "pizza" } };
dump(myList);
const std::string name = "pizza";
myList.remove_if([&](const A& it){ return it.name_ == name; });
dump(myList);
return 0;
}
Live demo: http://ideone.com/SaWejv
erase() invalidates the erased iterator. The for loop then attempts to increment the invalidated iterator, resulting in undefined behavior, and a crash.
The correct way to do this, actually, would be:
i=erase(i);
rather than using post-increment.
I'll avoid repeating what others have stated and instead suggest a more elegant solution via the erase-remove idiom:
myList.erase(std::remove_if(myList.begin(), myList.end(), [&name](auto& el) {
return el.first == name;
}), myList.end());
The reason is that the iterator breaks when you remove an item, but in the first case you are returning back so the error never occurs in the next iteration. In any way it's not a matter of your struct.

How to erase an element from a vector<shared_ptr>

I´m buiding a sort of a registry class using the following code:
class MyClass
{
public:
int a;
std::string b;
};
class Register
{
public:
std::vector<std::shared_ptr<MyClass>> items;
bool registerItem(std::shared_ptr<MyClass> item)
{
/*
* Check if item exists
*/
auto position = std::find(items.begin(), items.end(), item);
if (position != items.end())
return false;
items.push_back(item);
return true;
}
bool unregisterItem(std::shared_ptr<MyClass> item)
{
auto position = std::find(items.begin(), items.end(), item);
if (position == items.end())
return false;
items.erase(item);
return true;
}
};
int main()
{
std::shared_ptr<MyClass> item1 = new MyClass;
Register registry;
if (!registry.registerItem(item1))
std::cout << "Error registering item1" << std::endl;
else
std::cout << "Success registering item1" << std::endl;
if (!registry.registerItem(item1))
std::cout << "Error unregistering item1" << std::endl;
else
std::cout << "Success unregistering item1" << std::endl;
}
I can´t compile this code, as items.erase(item) complains about error: no matching member function for call to 'erase'.
Why I can´t erase the object I´ve added. What is the correct way to remove a std::shared_ptr from a std::vector ?
Because you want to use an iterator into items (i.e. position), so:
items.erase(position);
There are two declarations available. From cppreference:
iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);
You try to std::vector::erase the item itsself instead of the iterator. Use
items.erase(position);
instead.
You have to call erase on position, not item
since erase is used on iterators
You should pass to erase iterator not a value, there is nothing special with deleting std::shared_ptr. Also you have mistype in code: you register item twice instead of unregister it in the second call.

How to return null pointer for list member c++?

I need to return pointer for a member in a list, and if the required member is not there I get a null pointer (or any other indication)
list<LINKS>::iterator find_link(list<LINKS> &link, char* ID)
{
list<LINKS>::iterator link_index;
LINKS link_i;
link_index = link.begin();
for (int i = 0; i < link.size(), i++)
{
link_i = *link_index;
if (!strcmp(link_i.ID, ID)) { return link_index; };
link_index++;
};
cout << "Node outsession does not exist " << ID;
return nullptr; // ERROR (nullptr is not the same type as list<LINKS>::iterator)
};
I added the error message (cout << "Node outsession does not exist " << ID;) to indicate if the link is not found, but I need to indicate that for the caller function.
How can I do that ?
Thanks
nullptr is not an iterator, so you can't return it in a function that returns an iterator value
std::find(begin(), end(), value) returns an iterator and returns end() if the value is not found. To check it you say:
std::list<int> l;
...
auto found = std::find(l.begin(), l.end(), 6); // returns an std::list::iterator
if (found == l.end())
{
std::cout << "Bogus!" << std::endl;
}
...
There are other examples in which the container find returns a value, and uses a special value for "not found"
std::string s = "go to the store";
auto found = s.find('x'); // returns a size_t index into the string
if (found == std::string::npos)
{
std::cout << "Bogus!" << std::endl;
}
Returning nullptr would make sense if your function returned a pointer. In that case, nullptr would be a reasonable sentinel value for not found.
The normal accepted way to return 'value not found' when searching a list is to return an iterator pointing to the end of the list. In this case link.end()
This is exactly how the std::find algorithm from <algorithm> behaves.
In your calling function test the returned iterator as follows:
list<LINKS>::iterator itr = find_link(list, "Funyuns");
if( itr == list.end() ) {
std::cout << "You are all out of Funyons :-(" << std::endl;
}

Find value in vector and return reference to value

I'm using this function to find value in vector.
void program_data::find_curlAccountdata(int world, std::wstring login, std::wstring pass, curl_accounts & tmp){
std::unordered_map <int, std::vector<curl_accounts> > &t = curl_accountsdata; //world
std::vector<curl_accounts> &b = t[world];
std::vector<curl_accounts>::iterator & it = std::find_if(b.begin(), b.end(), [login, pass](curl_accounts& s) { return (s.name == login && s.pass == pass); });
if (it != b.end()){
tmp = *it;
}
}
And curl_accounts is struct.
Now when I edit some value in the struct in one place, and try to check it in second place it not the same.
So could you fix my function?
EDIT::
curl_accounts tmp,tmp2;
program_data::getInstance().find_curlAccountdata(server, login, pass, tmp);
tmp.logged = true;
std::cout << "TMP LOGGED: "<< tmp.logged<<std::endl; // return true
program_data::getInstance().find_curlAccountdata(server, login, pass, tmp2);
std::cout << "TMP 2 LOGGED: " << tmp2.logged << std::endl; // return false
std::cout << "sth";
You need a reference to a pointer. Try changing your signature to use curl_accounts *& tmp, instead of curl_accounts &tmp.