std

C++17 optional tree, error: invalid use of incomplete type

一笑奈何 提交于 2021-02-16 15:15:36
问题 When I compile a binary tree containing optional types: #include <optional> class BinaryTree { public: BinaryTree(); int value; std::optional<BinaryTree> left,right; }; int main() { return 0; } via g++ -std=c++17 -Wfatal-errors main.cpp I face with this error In file included from /usr/include/c++/7/bits/move.h:54:0, from /usr/include/c++/7/bits/stl_pair.h:59, from /usr/include/c++/7/utility:70, from /usr/include/c++/7/optional:36, from main.cpp:1: /usr/include/c++/7/type_traits: In

Call to implicitly deleted copy constructor

感情迁移 提交于 2021-02-11 12:13:04
问题 I have the following setup: class MyClass { public: static MyClass Clone(const MyClass& other) { return MyClass(other, 10); } static MyClass CreateNormal(int x, int y) { return MyClass(int x, int y); } // There are a few more factory functions. private: // Constructor 1 MyClass(int x, int y) : b_(x, y) {} // Constructor 2 MyClass(const MyClass& other, int c) : b_(other.b_, c) {} // And a lot more constructors. // NotMyClass has its copy constructor deleted. NotMyClass b_; } int main() {

What is the complexity of search in sorted std::list?

99封情书 提交于 2021-02-10 15:43:19
问题 What is the complexity of search in sorted std::list? I knew that complexity of search in sorted data is O(log n) if the data structure has random access. But since list doesn't have random access, what is it's complexity when it is sorted? 回答1: Well, it's O(N). Search is always O(N) in a std::list , sorted or not. Sorted collections help because you can know if you're too far, or not enough. But since you can't start anywhere but at one end of a list, it won't help. 回答2: It depends how you

Replacing boost with std implementation

南楼画角 提交于 2021-02-09 02:46:13
问题 What is the correct way to replace this: std::ostringstream buf; std::for_each(bd.begin(), bd.end(), buf << boost::lambda::constant(" ") << boost::lambda::_1); With an implementation that doesn't use boost? This is what I've tried: std::string backspace("&nbps;"); std::ostringstream buf; std::for_each(bd.begin(), bd.end(), buf << backspace << std::placeholders::_1); The second '<<' is underlined in red and I get the error message: error C2679: binary '<<' : no operator found which takes a

C++. Why std::cout << char + int prints int value?

白昼怎懂夜的黑 提交于 2021-02-08 21:17:04
问题 Let's say, we have: char x = 'a'; int y = 1; So, if you run: std::cout << x + y; It prints 98 instead of 'b'. As i see from here <<operator has only int parameter implementation. From now on i have 2 questions: After char + int operation what type is returned? Why there is no char parameter implementation, but std::cout << x still works as expected and prints char value? 回答1: Thanks to Fefux, Bo Persson and Matti Virkkunen answers are: From CPP Reference: Implicit conversions: arithmetic

C++. Why std::cout << char + int prints int value?

送分小仙女□ 提交于 2021-02-08 21:10:39
问题 Let's say, we have: char x = 'a'; int y = 1; So, if you run: std::cout << x + y; It prints 98 instead of 'b'. As i see from here <<operator has only int parameter implementation. From now on i have 2 questions: After char + int operation what type is returned? Why there is no char parameter implementation, but std::cout << x still works as expected and prints char value? 回答1: Thanks to Fefux, Bo Persson and Matti Virkkunen answers are: From CPP Reference: Implicit conversions: arithmetic

C++. Why std::cout << char + int prints int value?

廉价感情. 提交于 2021-02-08 21:04:42
问题 Let's say, we have: char x = 'a'; int y = 1; So, if you run: std::cout << x + y; It prints 98 instead of 'b'. As i see from here <<operator has only int parameter implementation. From now on i have 2 questions: After char + int operation what type is returned? Why there is no char parameter implementation, but std::cout << x still works as expected and prints char value? 回答1: Thanks to Fefux, Bo Persson and Matti Virkkunen answers are: From CPP Reference: Implicit conversions: arithmetic

Compilation Error: void value not ignored as it ought to be in std::queue::pop() [duplicate]

爷,独闯天下 提交于 2021-02-05 12:29:31
问题 This question already has answers here : Why doesn't std::queue::pop return value.? (7 answers) Closed 3 years ago . Write a function to connect all the adjacent nodes at the same level in a binary tree. Structure of the given Binary Tree node is like following. struct Node{ int data; Node* left; Node* right; Node* nextRight; } Initially, all the nextRight pointers point to garbage values. Your function should set these pointers to point next right for each node. My code is #include<queue> /*

Parsing int in C++11 - stoi

£可爱£侵袭症+ 提交于 2021-02-05 10:59:05
问题 I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std , but I am getting Function 'stoi' could not be resolved despitre using namespace std; #include <iostream> #include <string> #include <cstring> #include <fstream> #include<stdlib.h> using namespace std; int main(int argc, char* argv[]) { string line = ""; string five = "5"; int number = stoi(five); //Error

std::stod ignores nonnumerical values after decimal place

谁说我不能喝 提交于 2021-02-05 09:29:41
问题 I am reading in a value with a string, then converting it to a double. I expect an input such as 2.d to fail with std::stod, but it returns 2 . Is there a way to ensure that there is no character in the input string at all with std::stod? Example code: string exampleS = "2.d" double exampleD = 0; try { exampleD = stod(exampleS); // this should fail } catch (exception &e) { // failure condition } cerr << exampleD << endl; This code should print 0 but it prints 2 . If the character is before