How to find if a given key exists in a std::map
Use map::find and map::end: if (m.find(“f”) == m.end()) { // not found } else { // found }
Use map::find and map::end: if (m.find(“f”) == m.end()) { // not found } else { // found }
Personally, I would use the map, as its use implies a data lookup – using a switch usually indicates a difference in program behavior. Furthermore modifying the data mapping is easier with a map than with a switch. If performance is a real issue, profiling is the only way to get a usable answer. A … Read more
You can, if you use std::condition_variable_any, which allows for any type of object that supports the Lockable concept. However, in the case of recursive mutex, you do have to ensure that the given thread has only locked the recursive mutex once, since the condition variable only will use the unlock method on the unique_lock once … Read more
I won’t say it is the “best way”, but a way is to use std::string‘s iterator constructor: std::array<char, 10> arr; … // fill in arr std::string str(std::begin(arr), std::end(arr));
For a quick solution involving no external libraries, you can use hash<std::string> to hash strings. It’s defined by including the header files hash_map or unordered_map (or some others too). #include <string> #include <unordered_map> hash<string> hasher; string s = “heyho”; size_t hash = hasher(s); If you decide you want the added security of SHA, you don’t … Read more
This was a very contentious topic during the standardization of C++11. The problem is that all standard containers, including lists, also have a constant-time size operation. Before C++11, many implementations made size linear time and splice between different lists constant time. C++11 now requires that size be constant and splice be linear. The problem is … Read more
Actually, the code for pairs is exactly the same, since std::tuple has operator = with std::pair as an argument. num_letter = std::make_pair(10, ‘a’); std::tie(num, letter) = num_letter;
Theoretically vector has the properties you need. As you noted, actions that possibly do assignments to the contained type, including especially any sequence modifications (empace_back, push_back, insert etc.) are not supported if the elements are noncopyable and/or nonassignable. So to create a vector of noncopyable elements, you’d have to construct each element during vector construction. … Read more
gcc does not fully support std::thread yet: http://gcc.gnu.org/projects/cxx0x.html http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html Use boost::thread in the meantime. Edit Although the following compiled and ran fine for me with gcc 4.4.3: #include <thread> #include <iostream> struct F { void operator() () const { std::cout<<“Printing from another thread”<<std::endl; } }; int main() { F f; std::thread t(f); t.join(); return 0; … Read more
It is valid to move from optional<T>::value() since it returns a mutable reference and the move does not destroy the object. If the optional instance is not engaged, value() will throw a bad_optional_access exception (§20.6.4.5). You explicitly check whether the option is engaged: if (content) Process(move(*content)); But you don’t use the member value() to access … Read more