Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check a value exists in a c++ stl vector and apply a function to every element of the vector?

I have two questions related to the vector class of the standard library in C++.

  1. How can I check whether a value (let's say of an integer) already exists in a vector?

    What I want in words is as follows: "if the integer already exists in the vector, next one, else add it at the end of the vector."

  2. How do I apply a function that holds arguments to every element in the vector? (It seems I can't do that with for_each)

    In words: "for each z element in the vector apply MyAddFn(i,j)"

... or maybe I'm not on the right track with the stl vector sequence container, and I should define my own iterator?

like image 459
tagoma Avatar asked Dec 12 '22 00:12

tagoma


1 Answers

1)

std::find(v.begin(), v.end(), 5) == v.end() // checks that vector<int> v has no value 5.

2) Use new C++11 std::bind for example, but for real advice i need more context of use MyAddFn.

like image 103
ForEveR Avatar answered Feb 23 '23 00:02

ForEveR