std

How to return a class member vector in c++11

女生的网名这么多〃 提交于 2019-12-20 10:43:25
问题 I read a couple of posts on how to return a vector from a method include these ones: c11 rvalues and move semantics confusion return statement want speed pass by value why does visual studio not perform return value optimization rvo Wiki - Return Value Optimization and I'm still confused on how to pass a vector the right way in VS2013 and what are the differences between the following methods in this code(the questions are marked in the comments): class Foo{ private: std::vector<int> vect;

Obtaining list of keys and values from unordered_map

旧城冷巷雨未停 提交于 2019-12-20 08:55:58
问题 What is the most efficient way of obtaining lists (as a vector ) of the keys and values from an unordered_map ? For concreteness, suppose the map in question is a unordered_map<string, double> . I'd then like to obtain the keys as a vector<string> , and the values as a vector<double> . unordered_map<string, double> um; vector<string> vs = um.enum_keys(); vector<double> vd = um.enum_values(); I can just iterate across the map and collect the result, but is there a more efficient method? It

Get each value from a string via std::ifstream

假如想象 提交于 2019-12-20 06:13:30
问题 I am try to use an ifstream with the while loop to get each value. However, when I try it, nothing happens. Why? std::string line; std::getline(cin, line); std::ifstream stream(line); while(stream){ std::cout << stream.get(); } 回答1: You must use an istringstream , not an ifstream . 来源: https://stackoverflow.com/questions/20639889/get-each-value-from-a-string-via-stdifstream

c++ vector pointer to base class, and access to multiple derived classes methods

≡放荡痞女 提交于 2019-12-20 06:03:14
问题 class A{ public: int var; virtual int getVar() { return var; } }; class B: public A{ public: int anothervar; int getAnotherVar() { return anothervar; } }; class C: public A{ public: int finalvar; int getFinalVar() { return finalvar;} }; int main () { vector<A*> myvec; myvec.push_back (new B()); // implying all constructors are ok myvec.push_back (new C()); cout << myvec[0]->geVar(); // this works fine cout << myvec[0]->getAnotherVar(); // how can I do this ? cout << myvec[1]->getFinalVar(); /

C++ regex match content inside curly braces [duplicate]

瘦欲@ 提交于 2019-12-20 04:31:26
问题 This question already has answers here : Is gcc 4.8 or earlier buggy about regular expressions? (3 answers) Closed last year . Suppose I would like to do extract the contents of matching curly braces using C++11 regex. So, for example, {foo} would match successfully and I could the use the match_result to extract the contents. It seems simple, but the following code does not quite do what I wish std::string foo("{foo}"); std::regex r("\\{(.*)\\}"); std::smatch m; bool result = regex_match(foo

C++ regex match content inside curly braces [duplicate]

浪子不回头ぞ 提交于 2019-12-20 04:31:22
问题 This question already has answers here : Is gcc 4.8 or earlier buggy about regular expressions? (3 answers) Closed last year . Suppose I would like to do extract the contents of matching curly braces using C++11 regex. So, for example, {foo} would match successfully and I could the use the match_result to extract the contents. It seems simple, but the following code does not quite do what I wish std::string foo("{foo}"); std::regex r("\\{(.*)\\}"); std::smatch m; bool result = regex_match(foo

Getters/Setters with std::vector<>.push_back(…)

冷暖自知 提交于 2019-12-20 02:13:24
问题 For some reason this doesn't work. It compiles file, but no items are added to this vector when using a getter. E.G. class class_name { public: inline std::vector<int> get_numbers() { return m_numbers; } private: std::vector<int> m_numbers; } .... { class_name number_list; number_list.get_numbers().push_back(1); } If I do it directly (m_numbers.push_back(1)) it works, but if I pull it out with a getter it won't add anything. 回答1: Return the vector by reference if you plan to modify it: inline

Getters/Setters with std::vector<>.push_back(…)

99封情书 提交于 2019-12-20 02:13:19
问题 For some reason this doesn't work. It compiles file, but no items are added to this vector when using a getter. E.G. class class_name { public: inline std::vector<int> get_numbers() { return m_numbers; } private: std::vector<int> m_numbers; } .... { class_name number_list; number_list.get_numbers().push_back(1); } If I do it directly (m_numbers.push_back(1)) it works, but if I pull it out with a getter it won't add anything. 回答1: Return the vector by reference if you plan to modify it: inline

Sort a vector of vectors and count frequencies of unique occurences using the standard library

五迷三道 提交于 2019-12-20 02:09:17
问题 Given a std::vector<std::vector<int>> : I want to output a sorted std::vector<std::vector<int>> that contains only unique std::vector<int> as well as the frequency (i.e., count ) of these unique std::vector<int> My question is twofold: how can I efficiently achieve this relying only on the standard library? what is causing the code below to perform poorly? I tried the following: std::vector<std::vector<int>> responseVectors { { 2, 3, 2 }, { 3, 2, 3 }, { 3, 2, 3 }, { 3, 3, 3 }, { 1, 2, 3 }, {

std::max_element skip over NANs

江枫思渺然 提交于 2019-12-20 01:12:01
问题 I have a std::vector<double> that may contain several NAN values. I want to find the largest element in the vector. How can I efficiently skip the NAN s in the comparison? I'd like to avoid having to call isnan on each element. any ideas? // std::max_element([NAN,NAN,NAN,-31,-89]) = NAN // because NAN > -31 returns NAN. // how can I skip all NANs in the comparison? // test 2 below is my use case. #include <vector> #include <iostream> #include <cmath> void vector_max(std::vector<double> v,