std

std::vector constructor with two input arguments [duplicate]

淺唱寂寞╮ 提交于 2021-02-05 08:57:48
问题 This question already has answers here : What is array to pointer decay? (9 answers) Closed 2 years ago . I came across a piece of C++ code in one of my projects that initializes a vector with two inputs. One of the inputs is an existing array, and the other is the same array plus the array length. I found a similar piece of code on another site: // Create an array of string objects std::string arr[] = {"first", "sec", "third", "fourth"}; // Initialize vector with a string array std::vector

Complex literal 'i' used in function argument

放肆的年华 提交于 2021-02-04 21:58:47
问题 There seems to be a problem, using the literal i in C++ with std::complex . Consider the following code: std::complex<double> a = -1.0i * 42.0; std::complex<double> b = a + 1.0i; The second line fails to compile with: error: no match for ‘operator+’ (operand types are ‘std::complex<double>’ and ‘__complex__ double’) This also shows up when using the complex literal in function calls, e.g. std::exp<std::complex<double>>( 1.0i * 3.14159 ); How come the complex literal 1.0i is not convertible to

Issue with std::thread from c++11

亡梦爱人 提交于 2021-02-04 05:54:11
问题 I have some troubles trying to compile a program with multi-threading from the standard template library. It return me a obscure error when i try to compile the following program : #include <iostream> #include <thread> void foo() { std::cout << "Thread 1\n"; } int main(int argc, char** argv) { std::thread tr(foo); std::cout << "Main thread\n"; tr.join(); return 0; } I don't understand the error : /tmp/ccE8EtL1.o : In the function « std::thread::thread<void (&)()>(void (&)()) » : file.cpp:(

Issue with std::thread from c++11

﹥>﹥吖頭↗ 提交于 2021-02-04 05:51:57
问题 I have some troubles trying to compile a program with multi-threading from the standard template library. It return me a obscure error when i try to compile the following program : #include <iostream> #include <thread> void foo() { std::cout << "Thread 1\n"; } int main(int argc, char** argv) { std::thread tr(foo); std::cout << "Main thread\n"; tr.join(); return 0; } I don't understand the error : /tmp/ccE8EtL1.o : In the function « std::thread::thread<void (&)()>(void (&)()) » : file.cpp:(

Overload operator + for vector: namespace std

ぐ巨炮叔叔 提交于 2021-01-29 15:57:01
问题 I am trying to overload the operator + and += for std::vector, and what I do is namespace std { template<class T> vector<T> operator+(vector<T> x, vector<T> y) { vector<T> result; result.reserve(x.size()); for (size_t i = 0; i < x.size(); i++) result[i] = x[i] + y[i]; return result; } } But I assume this is bad practice, because clang-tidy warns me "Modification of std namespace can result in undefined behavior". Is there other better practice in overloading operator for STL classes? 回答1:

How do I read and parse input from a user that is comma separated by receiving an std::istream object in c++?

天涯浪子 提交于 2021-01-29 10:11:16
问题 I have a class in c++ called Airplane. I need to create a read function using std::istream that lets a user type after a prompt in the console a line that is comma separated. This line of input will then be split up using the commas and assigned to different private data members of the class. As an example, if the user types into the console "abc,12345,hello," then I would need to parse that line and assign abc to one variable, 12345 to another and hello to the last. I believe after the user

Could not convert from '<brace-enclosed initializer list> to

我的未来我决定 提交于 2021-01-28 07:09:04
问题 I know that has a lot of questions similar, but I saw them and none of them helped me, I think is that because mine is kind of different, and at the same time weird. I made another question and a member answered to me, but instead of using classes he used structs. and it's working perfectly, but when I try to put it as classes, using the same logic, this is what happen, the error: error: could not convert '{{"foo", "bar"}}' from '' to 'B' I tried, but I don't know what is happening. #include

How do I use std::string in a C++ Addon for Node.js?

。_饼干妹妹 提交于 2021-01-27 19:04:58
问题 I'm trying to wrap C++ objects for use in javascript as per the node.js documentation here: https://nodejs.org/api/addons.html#addons_wrapping_c_objects The Addon would build without error, and work properly when my object "AnObject" only had numeric attributes i.e "int32_t age;". When I added the attribute "std::string name;" to AnObject, "node-gyp configure" worked, however "node-gyp build" gave the following error: Wills-MacBook-Pro:cppObjectWrapping willalley$ node-gyp build [...] In file

why does c++ std::min can't use a static field as its parameter when compile on O0?

强颜欢笑 提交于 2021-01-27 14:22:29
问题 same code, compile with O0, it will report an error: //============================================================================ // Name : test.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <stdint.h> using namespace std; class foo{ static const int64_t MAX_THREAD_NUM = 10 * 1000; public: void test(); }; void foo:

std::out_of_range exception is not thrown

折月煮酒 提交于 2021-01-27 11:31:27
问题 // The following code works fine, throwing a std::out_of_range exception: std::vector<double> vd{ 1.5 }; try { int i{ -1 }; double d = vd.at(i); // exception is thrown } catch (std::out_of_range& re) { std::cout << "Exception is " << re.what() << std::endl; // invalid vector subscript } If I access vector elements in a for loop with an invalid index, no std::exception is thrown although I use .at() . Why is the std::out_of_range exception not thrown? // in a for loop, this does not throw the