Check if element is in the list contains

0 votes

I have a list of elements, say, integers, and I want to see if my variable (another integer) is one of the list's elements. 

In Python, I'd write:

my_list = [1,2,3,4] # elements
my_var = 3 # my variable
my_var in my_list # returns boolean

How does one go about doing this in C++? I considered using std::list, but there is no find method in it. Such a method can be found in the std::set structure.

More specifically, my program is given some unique ids (a list, a set, whatever) and I iterate over a long list of input data (ids) to see if they are included in the list (boolean value returned for each iteration step). And I'm not sure how to do it in C++.

Jun 2 in C++ by Nicholas
• 5,300 points
26 views

1 answer to this question.

0 votes
The simplest and quickest method.

You could also use a set, but constructing that set from your list may take longer than faster membership testing.

The only way to be certain is to perform well in benchmarking.

(This is also dependent on the operations required)
answered Jun 2 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Check if a string contains a string in C++

I've got a std::string variable. I'd want ...READ MORE

Jul 28 in C++ by Nicholas
• 5,300 points
6 views
0 votes
0 answers

What is the difference between std::list<std::pair> and std::map in C++ STL?

What distinguishes std::list<std::pair> from std::map? Does the ...READ MORE

1 day ago in C++ by Nicholas
• 5,300 points
2 views
0 votes
1 answer

What is the difference between public, private, and protected inheritance in C++?

To begin answering that question, let me characterise member accessors in my own terms.  If you already know this, proceed to the section "next:". I'm aware of three types of accessors: public, protected, and private. Let: class Base { public: ...READ MORE

answered Jul 11 in C++ by Damon
• 4,960 points
31 views
0 votes
0 answers

Check if a string is palindrome

I need to write a software that ...READ MORE

Jul 11 in C++ by Nicholas
• 5,300 points
10 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
206 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,080 points
1,416 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
810 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,720 points
642 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10 in C++ by Damon
• 4,960 points
47 views
0 votes
1 answer

Is 'If Else' statement indentation important or not in C++? [duplicate]

White space has no effect on the understanding of code in C and C++.  That is not to say that the programmer should be unconcerned about its misuse. The easiest method to demonstrate what the above code truly represents is to specify all of the inferred braces directly, as seen below.  The 'if then' or 'otherwise' clause only affects one line of code in the if statement with no brackets. This is one of the reasons why people strive to insist on 'proper coding standards' to guarantee that other people can clearly grasp the programmer's flow and meaning. while(c != cols) { ...READ MORE

answered Jun 27 in C++ by Damon
• 4,960 points
30 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP