What is the performance overhead of std::function?

There are, indeed, performance issues with std:function that must be taken into account whenever using it. The main strength of std::function, namely, its type-erasure mechanism, does not come for free, and we might (but not necessarily must) pay a price for that. std::function is a template class that wraps callable types. However, it is not … Read more

Converting std::__cxx11::string to std::string

Is it possible that you are using GCC 5? If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro. This commonly … Read more

How to find out if an item is present in a std::vector?

You can use std::find from <algorithm>: #include <algorithm> #include <vector> vector<int> vec; //can have other data types instead of int but must same datatype as item std::find(vec.begin(), vec.end(), item) != vec.end() This returns an iterator to the first element found. If not present, it returns an iterator to one-past-the-end. With your example: #include <algorithm> #include … Read more

Replace part of a string with another string

There’s a function to find a substring within a string (find), and a function to replace a particular range in a string with another string (replace), so you can combine those to get the effect you want: bool replace(std::string& str, const std::string& from, const std::string& to) { size_t start_pos = str.find(from); if(start_pos == std::string::npos) return … Read more

What’s the difference between “STL” and “C++ Standard Library”?

The “STL” was written by Alexander Stepanov in the days long before C++ was standardised. C++ existed through the 80s, but what we now call “C++” is the language standardised in ISO/IEC 14882:2014 (and earlier versions, such as ISO/IEC 14882:2011). The STL was already widely used as a library for C++, giving programmers access to … Read more

What does the namespace std add? (C++) [closed]

1 – All the entities (variables, types, constants, and functions) of the standard C++ library are declared within the std namespace. using namespace std; introduces direct visibility of all the names of the std namespace into the code. ref: http://www.cplusplus.com/doc/tutorial/namespaces/ 2 – Namespaces in C++ are most often used to avoid naming collisions. Although namespaces … Read more

Does my overload of operator

I am legitimately intrigued why both the topic you linked, and https://arne-mertz.de/2015/01/operator-overloading-common-practice/ or many others never talk about a left operand from another type than ostream. Because it does not really matter. What works for std::ostream works similar for other types. Sorry, but its a bit like getting an example of overloading operator+ for a … Read more