c++-standard-library

How to get IOStream to perform better?

牧云@^-^@ 提交于 2019-11-26 03:04:18
问题 Most C++ users that learned C prefer to use the printf / scanf family of functions even when they\'re coding in C++. Although I admit that I find the interface way better (especially POSIX-like format and localization), it seems that an overwhelming concern is performance. Taking at look at this question: How can I speed up line by line reading of a file It seems that the best answer is to use fscanf and that the C++ ifstream is consistently 2-3 times slower. I thought it would be great if we

Deleting elements from std::set while iterating

微笑、不失礼 提交于 2019-11-26 01:57:15
问题 I need to go through a set and remove elements that meet a predefined criteria. This is the test code I wrote: #include <set> #include <algorithm> void printElement(int value) { std::cout << value << \" \"; } int main() { int initNum[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; std::set<int> numbers(initNum, initNum + 10); // print \'0 1 2 3 4 5 6 7 8 9\' std::for_each(numbers.begin(), numbers.end(), printElement); std::set<int>::iterator it = numbers.begin(); // iterate through the set and erase

Writing your own STL Container

二次信任 提交于 2019-11-26 01:10:00
问题 Are there guidelines on how one should write new container which will behave like any STL container? 回答1: Here's a sequence pseudo-container I pieced together from § 23.2.1\4 Note that the iterator_category should be one of std::input_iterator_tag , std::output_iterator_tag , std::forward_iterator_tag , std::bidirectional_iterator_tag , std::random_access_iterator_tag . Also note that the below is technically more strict than required, but this is the idea. Note that the vast majority of the

What are the mechanics of short string optimization in libc++?

微笑、不失礼 提交于 2019-11-26 00:58:23
问题 This answer gives a nice high-level overview of short string optimization (SSO). However, I would like to know in more detail how it works in practice, specifically in the libc++ implementation: How short does the string have to be in order to qualify for SSO? Does this depend on the target architecture? How does the implementation distinguish between short and long strings when accessing the string data? Is it as simple as m_size <= 16 or is it a flag that is part of some other member

How to convert std::string to lower case?

谁都会走 提交于 2019-11-25 22:58:23
问题 I want to convert a std::string to lowercase. I am aware of the function tolower() , however in the past I have had issues with this function and it is hardly ideal anyway as use with a std::string would require iterating over each character. Is there an alternative which works 100% of the time? 回答1: Adapted from Not So Frequently Asked Questions: #include <algorithm> #include <cctype> #include <string> std::string data = "Abc"; std::transform(data.begin(), data.end(), data.begin(), []

What&#39;s the difference between “STL” and “C++ Standard Library”?

坚强是说给别人听的谎言 提交于 2019-11-25 22:18:49
问题 Someone brought this article to my attention that claims (I\'m paraphrasing) the STL term is misused to refer to the entire C++ Standard Library instead of the parts that were taken from SGI STL. (...) it refers to the \"STL\", despite the fact that very few people still use the STL (which was designed at SGI). Parts of the C++ Standard Library were based on parts of the STL, and it is these parts that many people (including several authors and the notoriously error-ridden cplusplus.com)