std

Is there a function like find_last_of in std but not in string?

那年仲夏 提交于 2019-12-24 05:49:29
问题 I know there is function string::find_last_of But I want to process a large chunk of char* data. I don't want to assign the char* to a string. I know there is a std function std::find But it only can find char* on positive sequence. Is there such a way to find in reverse order? If not in std, is there a way in boost? 回答1: std::find_end() Searches for the last subsequence of elements [s_first, s_last) in the range [first, last). For example #include <algorithm> char *data = ...; char *data_end

Are there faster hash functions for unordered_map/set in C++?

六月ゝ 毕业季﹏ 提交于 2019-12-24 05:48:44
问题 Default function is from std::hash. I wonder if there are better hash functions for saving computational time? for integer keys as well as string keys. I tried City Hash from Google for both integer and string keys, but its performance is a little worse than std::hash. 回答1: std::hash functions are already good in performance. I think you should try open source hash functions. Check this out https://github.com/Cyan4973/xxHash. I quote from its description: "xxHash is an Extremely fast Hash

Compiling C++ code on Mac

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 04:32:10
问题 I want to write very simple C++ programs on my Mac but I get errors. I don't have a lot of experience with C++ (and gcc) and the simple guides on the web also don't discuss this topic. Please have a look at my simple hello world program: erik2:~[03:38 pm]$ cat hw.cpp #include <iostream> int main () { cout<<"Hello World!"; return 0; } erik2:~[03:38 pm]$ gcc hw.cpp hw.cpp: In function ‘int main()’: hw.cpp:5: error: ‘cout’ was not declared in this scope Responding to the answers until now

Compiling C++ code on Mac

ε祈祈猫儿з 提交于 2019-12-24 04:32:09
问题 I want to write very simple C++ programs on my Mac but I get errors. I don't have a lot of experience with C++ (and gcc) and the simple guides on the web also don't discuss this topic. Please have a look at my simple hello world program: erik2:~[03:38 pm]$ cat hw.cpp #include <iostream> int main () { cout<<"Hello World!"; return 0; } erik2:~[03:38 pm]$ gcc hw.cpp hw.cpp: In function ‘int main()’: hw.cpp:5: error: ‘cout’ was not declared in this scope Responding to the answers until now

Error when using std::min “no matching function for call to ‘min(<brace-enclosed initializer list>)’”

余生颓废 提交于 2019-12-24 04:02:49
问题 Following https://stackoverflow.com/a/9424211/3368959 I am trying to compare three numbers: #include <iostream> int main() { std::cout << std::min({2,5,1}) << std::endl; return 0; } But the compiler gives me the error: error: no matching function for call to ‘min(<brace-enclosed initializer list>)’ However, the code compiles just fine when using std::min(std::min(2,5),1) But the first way should work with the c++11 standard. What could I be doing wrong? 回答1: As @BoBTFish suggested: In order

Create a directory for every element of a path if it does not exist

时光总嘲笑我的痴心妄想 提交于 2019-12-24 04:01:50
问题 In C++, I want to create a directory from a path "path/that/consists/of/several/elements" . Also I want to create all parent directories of that directory in case they are not existing. How to do that with std C++? 回答1: std::experimental::filesystem/std::filesystem (C++14/C++17) provides create_directories(). It creates a directory for every path element if it does not already exist. For that it executes create_directory() for every such element. #include <experimental/filesystem> #include

Error when using std::min “no matching function for call to ‘min(<brace-enclosed initializer list>)’”

南楼画角 提交于 2019-12-24 04:00:40
问题 Following https://stackoverflow.com/a/9424211/3368959 I am trying to compare three numbers: #include <iostream> int main() { std::cout << std::min({2,5,1}) << std::endl; return 0; } But the compiler gives me the error: error: no matching function for call to ‘min(<brace-enclosed initializer list>)’ However, the code compiles just fine when using std::min(std::min(2,5),1) But the first way should work with the c++11 standard. What could I be doing wrong? 回答1: As @BoBTFish suggested: In order

Find substring in string using locale

筅森魡賤 提交于 2019-12-24 02:30:10
问题 I need to find if a string contains a substring, but according to the current locale's rules. So, if I'm searching for the string "aba", with the Spanish locale, "cabalgar", "rábano" and "gabán" would all three contain it. I know I can compare strings with locale information (collate), but is there any built-in or starightforward way to do the same with find, or do I have to write my own? I'm fine using std::string (up to TR1) or MFC's CString 回答1: For reference, here is an implementation

How to make Objective-c class using std:vector made available to Swift classes

谁说胖子不能爱 提交于 2019-12-24 01:54:53
问题 If I attempt to include an objective-C class that utilizes std:vector in my project's Swift bridging header, in my class I get the error: #import <vector> Error! 'vector' file not found The bridging file in question is in my custom Framework. If I don't include the objective-c header in my bridging header, all compiles and works fine, but of course I can't access the class from Swift classes. How can I use this objective-c class in my Swift classes? 回答1: Swift only supports bridging to

Can different threads insert into a map if they always use different keys?

空扰寡人 提交于 2019-12-24 01:29:06
问题 I'm trying to design a message queue for an object. There is a set of X threads that can all send message (to be processed later) to this object. If I have a std::map<thread_id_t, message> , is this thread safe, assuming thread one only adds messages with a key of 1, thread 2 to key 2, etc..? 回答1: std::map is not thread safe for multiple simultaneous writers. One of the many reasons why STL maps are not thread safe is that the underlying implementation of an STL map is an AVL tree that needs