c++-standard-library

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

风格不统一 提交于 2019-11-27 00:55:19
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? 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 by seeks, there is no way to write elsewhere. They are specified as follows (in 27.5.3.1.4 of C++11): app

Idiomatic use of std::rel_ops

谁说胖子不能爱 提交于 2019-11-27 00:51:15
问题 What is the preferred method of using std::rel_ops to add the full set of relational operators to a class? This documentation suggests a using namespace std::rel_ops , but this seems to be deeply flawed, as it would mean that including the header for the class implemented in this way would also add full relational operators to all other classes with a defined operator< and operator== , even if that was not desired. This has the potential to change the meaning of code in surprising ways. As a

Is this correct usage of C++ 'move' semantics?

北战南征 提交于 2019-11-27 00:21:44
问题 Tonight I've been taking a look at some code I've been working on over the last few days, and began reading up on move semantics, specifically std::move. I have a few questions to ask you pros to ensure that I am going down the right path and not making any stupid assumptions! Firstly: 1) Originally, my code had a function that returned a large vector: template<class T> class MyObject { public: std::vector<T> doSomething() const; { std::vector<T> theVector; // produce/work with a vector right

Why do we need to tie std::cin and std::cout?

我的梦境 提交于 2019-11-26 23:19:21
问题 By default, the standard input device is tied together with the standard output device in the form: std::cin.tie (&std::cout); which guarantees that the output buffer has been flushed before input is invoked. So I try to untie them by using std::cin.tie(0) , but it seems that the result, has no difference with the tied one. #include<iostream> using namespace std; int main(int argc, char *argv[]) { char c; cin.tie(0) cout << "Please enter c:"; cin >> c; cout << c ; return 0; } Am I testing

Does std::vector::insert() invalidate iterators if the vector has enough room (created through reserve)?

柔情痞子 提交于 2019-11-26 22:11:00
问题 Answering How to self-copy a vector? has got me a bit confused about iterator invalidation. Some literature says "if you use insert, push_back, etc. consider all iterators invalid". Thats clear, it might cause the vector to grow which invalidates iterators. What about the special case where I know there is going to be enough room? first try: myvec.reserve(myvec.size()*3); //does this protect me from iterator invalidation? vector<string>::iterator it = myvec.end(); myvec.insert(myvec.end(),

What are the reasons that extending the std namespace is considered undefined behavior?

荒凉一梦 提交于 2019-11-26 21:48:40
问题 Why is adding names to the std namespace undefined behaviour? The obvious answer is "because the standard says so," e.g. in C++14 [namespace.std] 17.6.4.2.1/1: The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. ... However, I would be really interested in the reasons for this ruling. I can of course understand adding overloads of names already in std could break behaviour; but

How similar are Boost filesystem and the standard C++ filesystem libraries?

吃可爱长大的小学妹 提交于 2019-11-26 21:08:42
问题 I need a filesystem library for use with a C++11-capable compiler or a C++14-capable one - so it can't be be from C++17. Now, I know that the filesystem library going into C++17 is based based on Boost::Filesystem; but - are they similar enough for me to use the Boost library and then seamlessly switch to the standard version at a later time, without changing more than, say, a using statement? Or are there (minor/significant) differences between the two? I know that for the case of variant ,

Intersection of two `std::map`s

孤人 提交于 2019-11-26 18:26:02
问题 Given that I have two std::map s, say: map<int, double> A; map<int, double> B; I'd like to get the intersection of the two maps, something of the form: map<int, pair<double,double> > C; Where the keys are the values in both A and B and the value is a pair of the values from A and B respectively. Is there a clean way using the standard-library? 回答1: template<typename KeyType, typename LeftValue, typename RightValue> map<KeyType, pair<LeftValue, RightValue> > IntersectMaps(const map<KeyType,

How can I use a std::valarray to store/manipulate a contiguous 2D array?

半城伤御伤魂 提交于 2019-11-26 16:09:49
问题 How can I use a std::valarray to store/manipulate a 2D array? I'd like to see an example of a 2D array with elements accessed by row/column indices. Something like this pseudo code: matrix(i,j) = 42; An example of how to initialize such an array would also be nice. I'm already aware of Boost.MultiArray, Boost.uBlas, and Blitz++. Feel free to answer why I shouldn't use valarray for my use case. However, I want the memory for the multidimensional array to be a contiguous (columns x rows) block.

Why was std::pow(double, int) removed from C++11?

假如想象 提交于 2019-11-26 12:57:38
问题 While looking into Efficient way to compute p^q (exponentiation), where q is an integer and reviewing the C++98 and C++11 standards I noticed that apparently the std::pow(double, int) overload was removed in C++11. In C++98 26.5/6 it has the double pow(double, int); signature. In C++11 26.8 all I could find was overloads taking a pair of float , double , or long double , and an explicit note that in case of a mixture of parameter types integral&double, that the pow(double, double) overload