Why is the STL so heavily based on templates instead of inheritance?

The short answer is “because C++ has moved on”. Yes, back in the late 70’s, Stroustrup intended to create an upgraded C with OOP capabilities, but that is a long time ago. By the time the language was standardized in 1998, it was no longer an OOP language. It was a multi-paradigm language. It certainly … Read more

What is the purpose of std::make_pair vs the constructor of std::pair?

The difference is that with std::pair you need to specify the types of both elements, whereas std::make_pair will create a pair with the type of the elements that are passed to it, without you needing to tell it. That’s what I could gather from various docs anyways. See this example from http://www.cplusplus.com/reference/std/utility/make_pair/ pair <int,int> one; … Read more

How do I print the elements of a C++ vector in GDB?

With GCC 4.1.2, to print the whole of a std::vector<int> called myVector, do the following: print *(myVector._M_impl._M_start)@myVector.size() To print only the first N elements, do: print *(myVector._M_impl._M_start)@N Explanation This is probably heavily dependent on your compiler version, but for GCC 4.1.2, the pointer to the internal array is: myVector._M_impl._M_start And the GDB command to print … Read more

What really is a deque in STL?

A deque is somewhat recursively defined: internally it maintains a double-ended queue of chunks of fixed size. Each chunk is a vector, and the queue (“map” in the graphic below) of chunks itself is also a vector. There’s a great analysis of the performance characteristics and how it compares to the vector over at CodeProject. … Read more

Is std::vector so much slower than plain arrays?

Using the following: g++ -O3 Time.cpp -I <MyBoost> ./a.out UseArray completed in 2.196 seconds UseVector completed in 4.412 seconds UseVectorPushBack completed in 8.017 seconds The whole thing completed in 14.626 seconds So array is twice as quick as vector. But after looking at the code in more detail this is expected; as you run across … Read more

How to convert wstring into string?

As Cubbi pointed out in one of the comments, std::wstring_convert (C++11) provides a neat simple solution (you need to #include <locale> and <codecvt>): std::wstring string_to_convert; //setup converter using convert_type = std::codecvt_utf8<wchar_t>; std::wstring_convert<convert_type, wchar_t> converter; //use converter (.to_bytes: wstr->str, .from_bytes: str->wstr) std::string converted_str = converter.to_bytes( string_to_convert ); I was using a combination of wcstombs and tedious … Read more

Remove spaces from std::string in C++

The best thing to do is to use the algorithm remove_if and isspace: remove_if(str.begin(), str.end(), isspace); Now the algorithm itself can’t change the container(only modify the values), so it actually shuffles the values around and returns a pointer to where the end now should be. So we have to call string::erase to actually modify the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)