c++-standard-library

How to get IOStream to perform better?

我怕爱的太早我们不能终老 提交于 2019-11-26 12:41:35
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 could compile a repository of "tips" to improve IOStreams performance, what works, what does not.

GCC linker can't find standard library?

依然范特西╮ 提交于 2019-11-26 12:31:28
问题 I\'ve been developing a school project in XCode. The final product has to be submitted in source code with a makefile, so I wrote a makefile and to start compiling that way, to make sure I had a working copy. Here\'s my makefile: all: main.o StackList.o world.o Farm.o gcc main.o StackList.o world.o Farm.o -g -o Project1 main.o: gcc -g -c main.cpp StackList.o: gcc -g -c Stacklist.cpp world.cpp: gcc -g -c world.cpp Farm.cpp: gcc -g -c Farm.cpp clean: rm *.o Project1 Compiling each of the object

Why is there no transform_if in the C++ standard library?

做~自己de王妃 提交于 2019-11-26 10:48:41
问题 A use case emerged when wanting to do a contitional copy (1. doable with copy_if ) but from a container of values to a container of pointers to those values (2. doable with transform ). With the available tools I can\'t do it in less than two steps : #include <vector> #include <algorithm> using namespace std; struct ha { int i; explicit ha(int a) : i(a) {} }; int main() { vector<ha> v{ ha{1}, ha{7}, ha{1} }; // initial vector // GOAL : make a vector of pointers to elements with i < 2 vector

Android ndk std::to_string support

给你一囗甜甜゛ 提交于 2019-11-26 10:41:20
问题 I\'m using android NDK r9d and toolchain 4.8 but I\'m not able to use std::to_string function, compiler throws this error: error: \'to_string\' is not a member of \'std\' Is this function not supported on android ndk? I try APP_CPPFLAGS := -std=c++11 with no luck. 回答1: You can try LOCAL_CFLAGS := -std=c++11 , but note that not all C++11 APIs are available with the NDK's gnustl. Full C++14 support is available with libc++ ( APP_STL := c++_shared ). The alternative is to implement it yourself.

C++ std::vector vs array in the real world

独自空忆成欢 提交于 2019-11-26 09:37:25
问题 I\'m new to C++. I\'m reading \"Beginning C++ Through Game Programming\" by Michael Dawson. However, I\'m not new to programming in general. I just finished a chapter that dealt with vectors, so I\'ve got a question about their use in the real world (I\'m a computer science student, so I don\'t have much real-world experience yet). The author has a Q/A at the end of each chapter, and one of them was: Q: When should I use a vector instead of an array? A: Almost always. Vectors are efficient

C++ Filehandling: Difference between ios::app and ios::ate?

烂漫一生 提交于 2019-11-26 08:12:16
问题 What\'s the difference between ios::ate and ios:app when writing to a file. In my view, ios::app gives you the ability to move around in the file, whereas with ios::ate it can only read/write at the end of the file. Is this correct? 回答1: It’s the other way around. When ios::ate is set, the initial position will be the end of the file, but you are free to seek thereafter. When ios::app is set, all output operations are performed at the end of the file. Since all writes are implicitly preceded

Forward declare a standard container?

喜夏-厌秋 提交于 2019-11-26 07:44:01
问题 Is it possible to forward declare an standard container in a header file? For example, take the following code: #include <vector> class Foo { private: std::vector<int> container_; ... }; I want to be able to do something like this: namespace std { template <typename T> class vector; } class Foo { private: std::vector<int> container_; ... }; Can this be done? 回答1: Declaring vector in the std namespace is undefined behavior . So, your code might work, but it also might not, and the compiler is

std::queue iteration

大城市里の小女人 提交于 2019-11-26 05:58:37
问题 I need to iterate over std::queue . www.cplusplus.com says: By default, if no container class is specified for a particular queue class, the standard container class template deque is used. So can I somehow get to the queue\'s underlying deque and iterate over it? 回答1: If you need to iterate over a queue then you need something more than a queue. The point of the standard container adapters is to provide a minimal interface. If you need to do iteration as well, why not just use a deque (or

Writing your own STL Container

删除回忆录丶 提交于 2019-11-26 05:33:45
Are there guidelines on how one should write new container which will behave like any STL container? 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 "standard" functions are technically optional, due to the awesomeness that is iterators. template <class T,

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

ε祈祈猫儿з 提交于 2019-11-26 03:24:00
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 variable? (I imagine that m_size or part of it might also be used to store string data). I asked this