printf with std::string?

It’s compiling because printf isn’t type safe, since it uses variable arguments in the C sense1. printf has no option for std::string, only a C-style string. Using something else in place of what it expects definitely won’t give you the results you want. It’s actually undefined behaviour, so anything at all could happen. The easiest … Read more

Compelling examples of custom C++ allocators?

As I mention here, I’ve seen Intel TBB’s custom STL allocator significantly improve performance of a multithreaded app simply by changing a single std::vector<T> to std::vector<T,tbb::scalable_allocator<T> > (this is a quick and convenient way of switching the allocator to use TBB’s nifty thread-private heaps; see page 7 in this document)

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

Can you remove elements from a std::list while iterating through it?

You have to increment the iterator first (with i++) and then remove the previous element (e.g., by using the returned value from i++). You can change the code to a while loop like so: std::list<item*>::iterator i = items.begin(); while (i != items.end()) { bool isActive = (*i)->update(); if (!isActive) { items.erase(i++); // alternatively, i = … 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

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

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