std

libc++ - stop std renaming to std::__1?

十年热恋 提交于 2019-12-17 20:05:43
问题 After substantial effort getting clang and libc++ to compile, run, integrate with NetBeans, and even cross-compile to a 32-bit machine, I thought I had it all figured out! So I go to use some features that libstdc++ didn't have (the entire reason for turning my dev environment upside down), and discover... I can't actually do that. libc++ is installed, it works, and the compiled program (when it works) does require it. However, the compiler still tries to use libstdc++ versions at every

Whats the difference between std::condition_variable and std::condition_variable_any?

浪尽此生 提交于 2019-12-17 19:48:22
问题 I'm probably missing something obvious, but I can't see any difference between between std::condition_variable and std::condition_variable_any . Why do we need both? 回答1: std::condition_variable is more specialized, and therefore can be more efficient when you don't need the flexibility of std::condition_variable_any . From N3290 §30.5[thread.condition]/1 Class condition_variable provides a condition variable that can only wait on an object of type unique_lock<mutex> , allowing maximum

Constructors : difference between defaulting and delegating a parameter

给你一囗甜甜゛ 提交于 2019-12-17 18:57:07
问题 Today, I stumbled upon these standard declarations of std::vector constructors : // until C++14 explicit vector( const Allocator& alloc = Allocator() ); // since C++14 vector() : vector( Allocator() ) {} explicit vector( const Allocator& alloc ); This change can be seen in most of standard containers. A slightly different exemple is std::set : // until C++14 explicit set( const Compare& comp = Compare(), const Allocator& alloc = Allocator() ); // since C++14 set() : set( Compare() ) {}

Correct signature of / detect presence of Container::reserve()

拥有回忆 提交于 2019-12-17 18:55:14
问题 Given a type C which is an STL-conforming container, how do I correctly detect if C contains a member function reserve ? I tried the following approach (with GCC 4.6.3): template< typename C, typename = void > struct has_reserve : std::false_type {}; template< typename C > struct has_reserve< C, typename std::enable_if< std::is_same< decltype( &C::reserve ), void (C::*)( typename C::size_type ) >::value >::type > : std::true_type {}; This works for C being std::vector , but not for the

Why std::bind can be assigned to argument-mismatched std::function?

扶醉桌前 提交于 2019-12-17 18:38:17
问题 I have code as follows: #include <functional> #include <iostream> using namespace std; void F(int x) { cout << x << endl; } int main() { std::function<void(int)> f1 = std::bind(F, std::placeholders::_1); f1(100); // This works, will print 100. int x = 0; std::function<void()> f2 = std::bind(F, x); f2(); // This works, will print 0. std::function<void(int)> f3 = std::bind(F, x); f3(200); // BUT WHY THIS WORKS?????? It prints 0. return 0; } My compiler info is: Apple LLVM version 6.0 (clang-600

Search a vector of objects by object attribute

我的未来我决定 提交于 2019-12-17 18:02:00
问题 I'm trying to figure out a nice way to find the index of a certain object in a vector - by comparing a string to a member field in the object. Like this: find(vector.begin(), vector.end(), [object where obj.getName() == myString]) I have searched without success - maybe I don't fully understand what to look for. 回答1: You can use std::find_if with a suitable functor. In this example, a C++11 lambda is used: std::vector<Type> v = ....; std::string myString = ....; auto it = find_if(v.begin(), v

In C++ check if std::vector<string> contains a certain value [duplicate]

核能气质少年 提交于 2019-12-17 17:42:17
问题 This question already has answers here : How to find out if an item is present in a std::vector? (19 answers) Closed 6 years ago . Is there any built in function which tells me that my vector contains a certain element or not e.g. std::vector<string> v; v.push_back("abc"); v.push_back("xyz"); if (v.contains("abc")) // I am looking for one such feature, is there any // such function or i need to loop through whole vector? 回答1: You can use std::find as follows: if (std::find(v.begin(), v.end(),

how to find the intersection of two std::set in C++?

五迷三道 提交于 2019-12-17 17:34:14
问题 I have been trying to find the intersection between two std::set in C++, but I keep getting an error. I created a small sample test for this #include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int main() { set<int> s1; set<int> s2; s1.insert(1); s1.insert(2); s1.insert(3); s1.insert(4); s2.insert(1); s2.insert(6); s2.insert(3); s2.insert(0); set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end()); return 0; } The latter program does not generate

How to declare std::unique_ptr and what is the use of it?

南楼画角 提交于 2019-12-17 17:34:01
问题 I try to understand how std::unique_ptr works and for that I found this document. The author starts from the following example: #include <utility> //declarations of unique_ptr using std::unique_ptr; // default construction unique_ptr<int> up; //creates an empty object // initialize with an argument unique_ptr<int> uptr (new int(3)); double *pd= new double; unique_ptr<double> uptr2 (pd); // overloaded * and -> *uptr2 = 23.5; unique_ptr<std::string> ups (new std::string("hello")); int len=ups-

Splitting a line of a csv file into a std::vector?

南笙酒味 提交于 2019-12-17 16:51:11
问题 I have a function that will read a CSV file line by line. For each line, it will split the line into a vector. The code to do this is std::stringstream ss(sText); std::string item; while(std::getline(ss, item, ',')) { m_vecFields.push_back(item); } This works fine except for if it reads a line where the last value is blank. For example, text1,tex2, I would want this to return a vector of size 3 where the third value is just empty. However, instead it just returns a vector of size 2. How can I