Click here to Skip to main content
15,031,162 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get an index from my struct vector from find_if, using this vector structure
vector<body> inventory;

while implementing find_if like such,
std::vector<body>::iterator index = find_if(inventory.begin(), inventory.end(),
                   [&](const body &b) {return b.name == tempStr;}
               );

and I know there is something missing here, I am quite new to C++ still and can't put a finger on what exactly the issue is. I need index to either be an int itself, or for it to be able to be converted from vector to vector<int>. This is because I plan on using whatever index I get from find_if on replace_if.

What I have tried:

I have tried both dereferencing it (which to not my surprise didn't work) and using transform(), which also didn't amount to much, though I'm open to doing them again because I feel like either one could still be a solution. I already have replace_if all set up, I just can't figure out this issue. Any and all help would be greatly appreciated, thank you.
Posted
Updated 11-Jul-21 10:12am

1 solution

I think your question's title is misworded. Regardless, I think the answer is pointer arithmetic. That is, if you truly want an index. Dereferencing the iterator that is returned will give a pointer to the actual object. To get its index you need to subtract the address of the [0] item from the iterator's pointer value and then divide that difference by the size of a pointer.

Sometimes one can call replace_if directly because it essentially includes the functionality of find_if. I think you can even use the same lambda expression.
   
v2
Comments
Ford Frost28 11-Jul-21 16:15pm
   
How would you word the title then, if it's misleading? I didn't really know how else to word it if I am completely honest. I'll try your solution, but I figured to ask this before going ahead.
Rick York 11-Jul-21 16:25pm
   
You stated, "for it to be able to be converted from vector to vector<int>" Nowhere in your question did you describe what integers are to be placed in that vector. If what you want to do is find the index of an item, what does that have to do with a vector of integers? If you want to store a vector of the results of find_if then you do do not need an index because you can save a vector of iterators and access the items using the iterators much more simply. However, if you plan to delete items from a vector using the iterators then make sure you access them from last to first or else they will become invalid after the first deletion.
Ford Frost28 11-Jul-21 17:08pm
   
Could you give me an example on how to store a vector of the results of find_if then? I am struggling to understand how I would do that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900