Could you please recommend some C++ 20/17 textbook for beginner?
I want to learn C++ on my own this summer vacation, so could you please recommend some textbooks on C++ 20? I only shallowly learn some basic python before, so I want a thorough C++20 or C++17 textbook for an absolute beginner
do you know?
how many words do you know
See also questions close to this topic
-
C++ increment with macro
I have the following code and I want to use increment with macro:
#include <iostream> #define ABS(x) ((x) < 0 ? -(x) : (x)) int main(int argc, char** argv) { int x = 5; const int result = ABS(x++); std::cout << "R: " << result << std::endl; std::cout << "X: " << x << std::endl; return EXIT_SUCCESS; }
But output will be incorrect:
R: 6 X: 7
Is it possible to somehow use macros with an increment, or should this be abandoned altogether?
-
Can anyone pls tell me whats wrong with my code ? im stuck for the last 3 hours ,this question is bipartite graph in c++
idk why im getting error ,can someone help ? im trying to prove if a graph is bipartite or not in c++
bool isBipartite(vector<int> graph[],int V) { vector<int> vis(V,0); vector<int> color(V,-1); color[0]=1; queue <int> q; q.push(0); while (!q.empty()) { int temp = q.front(); q.pop(); for (int i=0;i<V;i++) { if (!vis[i] && color[i] == -1) "if there is an edge, and colour is not assigned" { color[i] = 1 - color[temp]; q.push(i); vis[i]=1; } else if (!vis[i] && color[i] == color[temp] "if there is an edge and both vertices have same colours" { vis[i]=1; return 0; // graph is not bipartite } } } return 1; }
it gives output "no" for whatever i enter
-
How to assign two or more values to a QMap Variable in Qt
I am getting confused of how to store the values assigned from 3 different functions and storing them in a single map variable
QMap<QString,QString> TrainMap = nullptr; if(......) ( TrainMap = PrevDayTrainMap(); TrainMap = NextDayTrainMap(); TrainMap = CurrentDayTrainMap(); }
The PrevDayTrainMap,NextDayTrainMap & CurrentDayTrainMap returns a set of values with Date and the TrainIdName.I need to store all the values from prevday,currentday and nextday in the TrainMap but it stores only the currentday values to the TrainMap as it is assigned at the last.I am not sure what to do so that it doesn't overwrite.If I should merge what is the way to do it?
-
std::stringstream's seekg does not work after while loop
I have this
std::stringstream
object whose contents I have to read (only) twice. For this I thought I could use itsseekg
member function to repeat the reading. But I can't make it work. Here's a MWE that reproduces the issues I'm having (adapted from the example in the docs).#include <iostream> #include <string> #include <sstream> int main() { const char *str = "Hello, world"; std::stringstream ss(str); std::string word1 = "asdf"; std::string word2 = "qwer"; std::string word3 = "zxcv"; std::string word4 = "yuio"; ss >> word1; ss.seekg(0); // rewind ss >> word2; while (ss >> word3) { } ss.seekg(0); // rewind ss >> word4; std::cout << "word1 = " << word1 << '\n' << "word2 = " << word2 << '\n' << "word3 = " << word3 << '\n' << "word4 = " << word4 << '\n'; }
I expect the contents to be:
word1 = Hello, word2 = Hello, word3 = world word4 = Hello,
since the call
ss.seekg(0)
after the while loop should putss
at the beginning of its contents. Why do I not get the expected output, but rather this?word1 = Hello, word2 = Hello, word3 = world word4 = yuio
This means that
word4
is never updated, thus the statementss >> word4
has no effect. But in this case, then thess.seekg(0)
does not have any effect either.Is the while loop causing the
ss
object to fail to "rewind"? Perhaps thess
tried to read too much and now is in an error state (I have no idea).How can I fix this? That is, how can I make the statement
ss >> word4
readHello,
?I'm using
g++ (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0
on C++17. -
Recursive function - Best solution
Gentlemen,
I need to create a function that performs the tasks:
- http call;
- Save the answer in a text file (if successful);
- Wait 5 seconds (Sleep);
- Runs the same task again.
In this case, I think a "recursive function" could meet my need, but I see some people saying that we should avoid recursive functions.
Could you give me ideas for a secure implementation?
Thank you! Marcelo
-
Datetime parse and format in C++
I'm using time_point the first time. I want to parse datetime from string and found a difference when returning back to string from 1 hour.
std::chrono::system_clock::time_point timePoint; std::stringstream ss("2021-01-01 00:00:09+01"); std::chrono::from_stream(ss, "%F %T%z", timePoint); // timePoint == {_MyDur={_MyRep=16094556090000000 } } std::string timePointStr = std::format("{:%Y/%m/%d %T}", floor<std::chrono::seconds>(timePoint)); // timePointStr = "2020/12/31 23:00:09"
I don't know what is wrong: the timepoint and the parsing or the formatting to string? How can I get the same format as the parsed one?