std

C++ split std list into two lists

爷,独闯天下 提交于 2019-12-25 18:24:46
问题 Hey so I'm reasonably new into c++ and I ran into this problem where I want to split one std list of strings into two lists. For example: list(1,2,3,4) -> list1(1,2) & list2(3,4) I guess splice is what I am supposed to use for this, but I could not understand how that works at all... Can someone please advice me how to do this? Sorry about my bad English and thanks for help everyone. 回答1: "I'm reasonably new into c++" It's a common misconception of users coming with Java or C# experience,

C++ - std::map Alternative that doesn't require casting

社会主义新天地 提交于 2019-12-25 07:26:46
问题 I am using an std::map to store certain objects. The map has the template <Coordinate, Object> . Now, what I noticed is that the map casts the Coordinate to an integer, and then based on that gives the element a unique key. (Equal to that integer) Now, the problem is that it's impossible to convert a 3 dimensional integer (x, y, z) to a single integer, that the std::map can use. What alternatives are there to std::map which do require the key object to be unique, but don't require it to be

getline(cin, string) not working EVEN WITH cin.ignore()

丶灬走出姿态 提交于 2019-12-25 03:34:17
问题 There are several questions on this site referring to getline not working, the apparent solution is that there are remaining newline characters in the buffer that need to be cleared, supposedly with cin.ignore() . I've tried so many variations of this and nothing seems to work, all I'm trying to do is simple console input and cin >> string isn't an option because the string might have spaces. Here is my code. void prompt(std::string * str) { cout << "> "; cin.sync(); cin.get(); cin.ignore(256

Efficient intersection of sets?

一笑奈何 提交于 2019-12-25 01:53:56
问题 I'm wondering what the most efficient way of doing this is. I have points that I gather from 2 places. I am only interested in the points which are common to both places. My plan is to have 3 std::set<Point> . First I will add in the points from area A,into set A then points from B into set B and let set C be the intersection of both sets. However, I'm wondering if there is a better way of doing this that involves maybe less sets? Thanks 回答1: Your problem is so common that there even is a

Why would I want to lock two mutexes in one function - that too with deferred lock?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 18:13:07
问题 https://en.cppreference.com/w/cpp/thread/lock_tag void transfer(bank_account &from, bank_account &to, int amount) { // lock both mutexes without deadlock std::lock(from.m, to.m); // make sure both already-locked mutexes are unlocked at the end of scope std::lock_guard<std::mutex> lock1(from.m, std::adopt_lock); std::lock_guard<std::mutex> lock2(to.m, std::adopt_lock); // equivalent approach: // std::unique_lock<std::mutex> lock1(from.m, std::defer_lock); // std::unique_lock<std::mutex> lock2

unrecognized command line option '-stdlib=libc++' gcc (Homebrew gcc 5.3.0) 5.3.0

百般思念 提交于 2019-12-24 18:03:32
问题 I run Mac OSX El Capitan, I have installed via Homebrew gcc version 5.3.0. I want to install pyopencl (but as I understand it doesn't matter) and when running the following command: gcc -fno-strict-aliasing -fwrapv -Wall -O3 -DNDEBUG -DPYGPU_PACKAGE=pyopencl -DPYGPU_PYOPENCL=1 -Isrc/c_wrapper/ -I/Users/earendilllock/anaconda/include/python2.7 -c build/temp.macosx-10.5-x86_64-2.7/pyopencl._cffi.cpp -o build/temp.macosx-10.5-x86_64-2.7/build/temp.macosx-10.5-x86_64-2.7/pyopencl._cffi.o -std=c+

Constructing std::string using << operator

爱⌒轻易说出口 提交于 2019-12-24 18:02:09
问题 If we want to construct a complex string, say like this: "I have 10 friends and 20 relations" (where 10 and 20 are values of some variables) we can do it like this: std::ostringstream os; os << "I have " << num_of_friends << " friends and " << num_of_relations << " relations"; std::string s = os.str(); But it is a bit too long. If in different methods in your code you need to construct compound strings many times you will have to always define an instance of std::ostringstream elsewhere. Is

Constructing std::string using << operator

☆樱花仙子☆ 提交于 2019-12-24 18:01:23
问题 If we want to construct a complex string, say like this: "I have 10 friends and 20 relations" (where 10 and 20 are values of some variables) we can do it like this: std::ostringstream os; os << "I have " << num_of_friends << " friends and " << num_of_relations << " relations"; std::string s = os.str(); But it is a bit too long. If in different methods in your code you need to construct compound strings many times you will have to always define an instance of std::ostringstream elsewhere. Is

Can't submit callble objects that return void to a thread pool, but only callable objects that returns values

♀尐吖头ヾ 提交于 2019-12-24 11:38:02
问题 I'm working on a thread pool from the book C++ Cuncerrency in Action by Anthony Willimas This thread pools has a submit call that take as tasks callable objects that return a value and return a std::future handle to them, and I managed to build applications that use it. But I can't manage to make it work with callable ojects that return void: the code won't even compile. I get these errors, all in the future header : error C2182: '_Get_value' : illegal use of type 'void' error C2182: '_Val' :

C++ std::vector in constructor

荒凉一梦 提交于 2019-12-24 09:40:03
问题 I am trying to code an effective implementation of the following composite class: class composite{ vector<base_class *> Vec; //Other useful constants public: composite(vector<base_class*>); //Other useful operations... }; My question is about the constructor and instantiation of the class and in particular the object Vec. At the minute, I use the rather crude implementation outlined below. I the implementation to be memory efficient. I'm pretty much a newb with C++, so I'm not sure I have the