std

std::out_of_range exception is not thrown

醉酒当歌 提交于 2021-01-27 11:31:06
问题 // 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

How to initialize elements of an array managed by a unique_ptr?

白昼怎懂夜的黑 提交于 2021-01-27 07:07:26
问题 We know that Resource Acquisition is Initialization (RAII), I looked for the syntax to initialize an array of objects that have parameters (with no default params), managed by unique_ptr but I did not find any example, there is one in Cppreference constructing int int size = 10; std::unique_ptr<int[]> fact(new int[size]); How could I write like this: class Widget { Widget(int x, in y):x_(x),y_(y) {} int x_,y_; }; std::unique_ptr<Widget[]> fact(new Widget[size]); 回答1: Following the last answer

no member named 'str' in 'std::basic_ostream<char>' with gcc and clang, but no problem with msvc

筅森魡賤 提交于 2021-01-27 06:36:03
问题 This code snippet (https://gcc.godbolt.org/z/hKDMxm): #include <iostream> #include <sstream> using namespace std; int main() { auto s = (ostringstream{} << "string").str(); cout << s; return 0; } compiles and runs as expected with msvc, but fails to compile with clang 9.0.0 and gcc 9.2 giving this error message: no member named 'str' in 'std::basic_ostream<char>' . Looking at https://en.cppreference.com/w/cpp/io/basic_ostringstream/str there is clearly str() member of ostringstream . Why

no member named 'str' in 'std::basic_ostream<char>' with gcc and clang, but no problem with msvc

你说的曾经没有我的故事 提交于 2021-01-27 06:35:46
问题 This code snippet (https://gcc.godbolt.org/z/hKDMxm): #include <iostream> #include <sstream> using namespace std; int main() { auto s = (ostringstream{} << "string").str(); cout << s; return 0; } compiles and runs as expected with msvc, but fails to compile with clang 9.0.0 and gcc 9.2 giving this error message: no member named 'str' in 'std::basic_ostream<char>' . Looking at https://en.cppreference.com/w/cpp/io/basic_ostringstream/str there is clearly str() member of ostringstream . Why

no member named 'str' in 'std::basic_ostream<char>' with gcc and clang, but no problem with msvc

自闭症网瘾萝莉.ら 提交于 2021-01-27 06:35:10
问题 This code snippet (https://gcc.godbolt.org/z/hKDMxm): #include <iostream> #include <sstream> using namespace std; int main() { auto s = (ostringstream{} << "string").str(); cout << s; return 0; } compiles and runs as expected with msvc, but fails to compile with clang 9.0.0 and gcc 9.2 giving this error message: no member named 'str' in 'std::basic_ostream<char>' . Looking at https://en.cppreference.com/w/cpp/io/basic_ostringstream/str there is clearly str() member of ostringstream . Why

How does std::sort work for list of pairs?

不问归期 提交于 2021-01-22 03:57:01
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

How does std::sort work for list of pairs?

折月煮酒 提交于 2021-01-22 03:56:52
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

How does std::sort work for list of pairs?

僤鯓⒐⒋嵵緔 提交于 2021-01-22 03:53:57
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

How does std::sort work for list of pairs?

泄露秘密 提交于 2021-01-22 03:52:46
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

Why does std::min only support initializer_list?

允我心安 提交于 2021-01-20 09:16:04
问题 We can use std::min in below way: // 1. int a = 1, b = 2; std::min(a, b); // 2. std::min({1,2,3,4}); But why can't use a std::vector or std::list , Because the param in the template is initializer_list . template <class T, class Compare> pair<T,T> minmax (initializer_list<T> il, Compare comp); What is the reason for this design? 回答1: To explain "why it doesn't accept a container", take the semantic into consideration: std::min({ "foo", "bar", "hello" }) The semantic of std::min() means "find