Why does C++ containers not have a contains method? [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Checking membership is one of the most frequently used operations in any programming languages, but C++ insists that programmers use the verbose container.find(value) != container.end() instead of the readable container.contains(value). What is the rationale behind not adding a syntactic sugar for this operation?

This is not true for all containers. For example; std::set has a .count() member function, that is essentially the same as contains, in that it will return 0 if the container does not contain an element and >0 if it does. There's also std::any_of which works for all containers.

There is a method to check if a container contains an object: std::any_of(). This is also more flexible since it takes a predicate to determine if the object is in the container.

Related

Finding indexed data-structure like `std::vector` (not array) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am looking for a data structure which can store values with index and also can be resized like a std::vector (but should be indexed so I can access it easily) is there any C++ standard library implementation of my problem?
What I am looking for is an array-type DS from which I can remove elements.
You can use std::deque which is also indexed sequence container like std::vector. It provids also std::deque::resize member function.
However, your requirement should be much more specific to suggest std::deque at first place than std::vector

C++ STL doubly linked list performance comparison [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I would like to know if implementing my own Doubly Linked List is better or using stl list.h ?
in terms of efficiency and what would be the advantages and disadvantages of both ?
Thanks
The only way to know is to profile. Here are some tools.
As already said in the comments, unless this is an educational exercise you probably don't want to write your own.
There is an alternative to std::list, Boost.Intrusive which may have better performance than std::list.

Why does std::vector not have an append method? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
The recommendation for appending one vector to another (according to Concatenating two std::vectors) is to use:
vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
Why does vector not have a method along the lines of:
vector1.append(vector2);
The only reason that comes to mind is that it may be unclear exactly what append does.
That's a pretty good reason. In fact, as an apt example, I think you've got the meaning of "append" wrong. I'd expect it to be equivalent to push_back (which exists), whereas you're looking for something more like "concatenate".
Why isn't there a concatenate function? Well, there could have been. But the standard doesn't like to give you things you don't really need, and (as you've shown) this operation is pretty easy to implement yourself.

Is there any best practice for getting the first character of a string in C++? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
For years I have used stringname[0] to obtain the first char of a string without even thinking about it. However, I recently came to wonder if brute access to the array is really a good practice. This may seem a trivial question, but it is not because it looks deeply linked to memory/access management of STL containers.
I can think of stringname.at(0) (not very convincing) but there are probably better alternatives with an iterator. Most importantly, the ideal method would not cause an error if the string is empty.
Any widely accepted good practice for this ?
If s is an empty string, s[0] returns '\0' whereas s.at(0) throws std::out_of_range.
That difference in behavior is far more significant than any difference in performance.

Should a class be thread-safe? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Should a thread-safe mechanism be added when a class is developed and it is known that this class will be used in multi-threaded environment (not always however) or leave it to the user?
As a general rule, it's more flexible to leave it to the user. For example, consider a map-type container. Suppose the application needs to atomically move something from one map to another map. In this case, the user needs to lock both maps before the insert-erase sequence.
Having such a scenario be automatically taken care of somehow by your class would probably be inelegant, because it's naturally something that happens across objects, and because there may be many such scenarios, each slightly different.