std

C++ Long switch statement or look up with a map?

﹥>﹥吖頭↗ 提交于 2019-12-21 06:47:23
问题 In my C++ application, I have some values that act as codes to represent other values. To translate the codes, I've been debating between using a switch statement or an stl map. The switch would look something like this: int code; int value; switch(code) { case 1: value = 10; break; case 2: value = 15; break; } The map would be an stl::map<int, int> and translation would be a simple lookup with the code used as the key value. Which one is better/more efficient/cleaner/accepted? Why? 回答1:

Most efficient way to assign values to maps

旧巷老猫 提交于 2019-12-21 03:44:21
问题 Which way to assign values to a map is most efficient? Or are they all optimized to the same code (on most modern compilers)? // 1) Assignment using array index notation Foo["Bar"] = 12345; // 2) Assignment using member function insert() and STL pair Foo.insert(std::pair<string,int>("Bar", 12345)); // 3) Assignment using member function insert() and "value_type()" Foo.insert(map<string,int>::value_type("Bar", 12345)); // 4) Assignment using member function insert() and "make_pair()" Foo

push_back() and emplace_back() behind the scenes

ⅰ亾dé卋堺 提交于 2019-12-21 03:43:15
问题 I'm currently learning C++ on my own, and I am curious about how push_back() and emplace_back() work under the hood. I've always assumed that emplace_back() is faster when you are trying to construct and push a large object to the back of a container, like a vector. Let's suppose I have a Student object that I want to append to the back of a vector of Students. struct Student { string name; int student_ID; double GPA; string favorite_food; string favorite_prof; int hours_slept; int birthyear;

std::bind to std::function?

只谈情不闲聊 提交于 2019-12-21 03:19:10
问题 I get a compile error using this: std::vector<std::function<int(int)>> functions; std::function<int(int, int)> foo = [](int a, int b){ return a + b; }; std::function<int(int)> bar = std::bind(foo, 2); functions.push_back(bar); The error is: /usr/include/c++/4.6/functional:1764:40: error: no match for call to '(std::_Bind(int)>) (int)' Can someone tell me how to convert a std::bind into a std::function ? 回答1: std::function<int(int)> bar = std::bind(foo, 2, std::placeholders::_1); 来源: https:/

Should I free/delete char* returned by getenv()?

孤者浪人 提交于 2019-12-21 03:09:23
问题 char * val; val = getenv("ENV_VAR_NAME"); above is a code to get environment variable, will it cause memory leak if I dont free memory returned by getenv(char*) ? If no then please answer why? 回答1: No you shouldn't. Standard 7.20.4.5 says : The getenv function returns a pointer to a string associated with the matched list member. The string pointed to shall not be modified by the program , but may be overwritten by a subsequent call to the getenv function. I believe deletion is covered by the

Which std::sync::atomic::Ordering to use?

十年热恋 提交于 2019-12-21 03:09:11
问题 All the methods of std::sync::atomic::AtomicBool take a memory ordering (Relaxed, Release, Acquire, AcqRel, and SeqCst), which I have not used before. Under what circumstances should these values be used? The documentation uses confusing “load” and “store” terms which I don’t really understand. For example: A producer thread mutates some state held by a Mutex, then calls AtomicBool:: compare_and_swap(false, true, ordering) (to coalesce invalidations), and if it swapped, posts an “invalidate”

Can I include iostream header file into custom namespace? [closed]

老子叫甜甜 提交于 2019-12-21 02:46:30
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . namespace A { #include <iostream> }; int main(){ A::std::cout << "\nSample"; return 0; } 回答1: Short answer: No. Long answer: Well, not really. You can fake it, though. You can declare it outside and use using

Why can't you take the address of nullptr?

余生颓废 提交于 2019-12-20 10:59:23
问题 In the C++11 standard, I don't understand the reason why taking the address of nullptr is disallowed whereas one is allowed to take the address of their own std::nullptr_t instances. Aside from the fact that nullptr is a reserved keyword, is there any designated reasoning for this decision? Simply because it amuses me, I attempted to circumnavigate this restriction with the following function: decltype(nullptr)* func(const decltype(nullptr) &nref) noexcept { return const_cast<decltype(nullptr

Why can't you take the address of nullptr?

ε祈祈猫儿з 提交于 2019-12-20 10:59:13
问题 In the C++11 standard, I don't understand the reason why taking the address of nullptr is disallowed whereas one is allowed to take the address of their own std::nullptr_t instances. Aside from the fact that nullptr is a reserved keyword, is there any designated reasoning for this decision? Simply because it amuses me, I attempted to circumnavigate this restriction with the following function: decltype(nullptr)* func(const decltype(nullptr) &nref) noexcept { return const_cast<decltype(nullptr

std::thread problems

為{幸葍}努か 提交于 2019-12-20 10:43:59
问题 I think I have a kind really bad concepts problems. Why I simple get a lot of race conditions error with valgrind. First i thought that could be a bug, and I saw in forums that an updated rolling release of linux will solve this... so now i have opensuse tubleweed, 100% updated. The following code muss have something very wrong: #include <iostream> #include <thread> using namespace std; class FOO { public: void do_something () { cout<<"cout somethin\n"; } }; int main () { FOO foo; std::thread