c++-standard-library

Idiomatic use of std::rel_ops

∥☆過路亽.° 提交于 2019-11-28 05:41:05
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 side note - I have been using Boost.Operators to do this, but I am still curious about the standard

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

偶尔善良 提交于 2019-11-28 04:21:50
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 here return(theVector); }; // eo doSomething }; // eo class MyObject Given "theVector" is temporary in

Get index by type in std::variant

半世苍凉 提交于 2019-11-28 02:59:40
问题 Is there a utility in the standard library to get the index of a given type in std::variant ? Or should I make one for myself? That is, I want to get the index of B in std::variant<A, B, C> and have that return 1 . There is std::variant_alternative for the opposite operation. Of course, there could be many same types on std::variant 's list, so this operation is not a bijection, but it isn't a problem for me (I can have first occurrence of type on list, or unique types on std::variant list).

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

泄露秘密 提交于 2019-11-28 02:18:39
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(), myvec.begin(), it); myvec.insert(myvec.end(), myvec.begin(), it); After some excellent answers second try

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

梦想与她 提交于 2019-11-27 23:33:26
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 wrong? Why do we need to tie them together? Do they share the same buffer? user1284631 There is nothing

What should I use instead of sscanf?

廉价感情. 提交于 2019-11-27 20:53:51
问题 I have a problem that sscanf solves (extracting things from a string). I don't like sscanf though since it's not type-safe and is old and horrible. I want to be clever and use some more modern parts of the C++ standard library. What should I use instead? 回答1: Try std::stringstream: #include <sstream> ... std::stringstream s("123 456 789"); int a, b, c; s >> a >> b >> c; 回答2: For most jobs standard streams do the job perfectly, std::string data = "AraK 22 4.0"; std::stringstream convertor(data

Assign a nullptr to a std::string is safe?

狂风中的少年 提交于 2019-11-27 19:06:58
问题 I was working on a little project and came to a situation where the following happened: std::string myString; #GetValue() returns a char* myString = myObject.GetValue(); My question is if GetValue() returns NULL myString becomes an empty string? Is it undefined? or it will segfault? 回答1: Interesting little question. According to the C++11 standard, sect. 21.4.2.9, basic_string(const charT* s, const Allocator& a = Allocator()); Requires: s shall not be a null pointer. Since the standard does

std::stoi missing in g++ 4.7.2?

天大地大妈咪最大 提交于 2019-11-27 16:28:43
问题 I get the error message "stoi is not a member of std" when I try to use std::stoi and try to compile it. I'm using g++ 4.7.2 from the command line so it can't be IDE error, I have all my includes in order, and g++4.7.2 defaults to using c++11. If it helps, my OS is Ubuntu 12.10. Is there something I haven't configured? #include <iostream> #include <string> using namespace std; int main(){ string theAnswer = "42"; int ans = std::stoi(theAnswer, 0, 10); cout << "The answer to everything is " <<

Assign a nullptr to a std::string is safe?

青春壹個敷衍的年華 提交于 2019-11-27 15:59:26
I was working on a little project and came to a situation where the following happened: std::string myString; #GetValue() returns a char* myString = myObject.GetValue(); My question is if GetValue() returns NULL myString becomes an empty string? Is it undefined? or it will segfault? Interesting little question. According to the C++11 standard, sect. 21.4.2.9, basic_string(const charT* s, const Allocator& a = Allocator()); Requires: s shall not be a null pointer. Since the standard does not ask the library to throw an exception when this particular requirement is not met, it would appear that

Intersection of two `std::map`s

守給你的承諾、 提交于 2019-11-27 14:43:37
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? Mark Ransom template<typename KeyType, typename LeftValue, typename RightValue> map<KeyType, pair<LeftValue, RightValue> > IntersectMaps(const map<KeyType, LeftValue> & left, const map<KeyType, RightValue> & right) { map<KeyType, pair<LeftValue,