Anonymous
Chapter 6 "Circulation" took 2 days and I couldn't understand
Leo
In my opinion, that book isn't good for beginers.
Anonymous
I think so too. It's too comprehensive. I just want to lay some foundation of C language for postgraduate computer
Nils
Hi, can I split a string by fast space? So "Hello nice world!" turns into: {"Hello", "nice world!"}
Nils
std::list<std::string> split(std::string s, char delimiter, int times) { std::string currpart; std::list<std::string> splitted; int timesdone = 0; for (std::string::iterator strit = s.begin(); strit != s.end(); strit++) { currpart.push_back(*strit); if (*strit == delimiter and timesdone < times) { splitted.push_back(currpart); currpart = ""; times++; } } splitted.push_back(currpart); return splitted; } (Or: https://hastebin.com/kulujukula.cpp) Do you think this is okay?
Nils
What does the "times" parameter do?
It says how many times the string shoulds be split by delimiter
Francisco
Well, you don't use the parameter, so...
Nils
sure I do
Francisco
Oh okay, you edited it
Nils
Yep, sorry
Anonymous
@leoleoecdsa_bot Thanks, I am already watching mooc
Nils
auto cmdsplit = strsplit(commandstr, ' ', 1); std::string command = cmdsplit[0]; std::string argsstr = cmdsplit[1]; Is there any better way to solve this?
......
yes actually
What you mean?
klimi
What you mean?
what do you mean
Nils
What do you want to achieve?
First element of list in variable command, second one in variable argsstr
Nils
strsplit() returns std::list<std::string>
Nils
Why are you using list?
Because it returns a list, lol
Francisco
strsplit() returns std::list<std::string>
Keep in mind that std::list is not random access, so you can't do stuff like my_list[i];
Nils
oh.
Nils
but nvm I solved it using iterator
Francisco
Also, why are you using lists instead of vectors?
Francisco
There're very few situations where lists actually beat vectors performance-wise
Nils
Are vectors used exactly the same as lists?
Anonymous
it is not intended to be modified
ye so don't copy it. just use string_view as the parameter
Anonymous
clangd should warn about these things
Nils
strsplit(std::basic_string_view s, char delimiter, int times) Like this?
Anonymous
strsplit(std::basic_string_view s, char delimiter, int times) Like this?
just std::string_view or std::basic_string_view<char>
Nils
just std::string_view or std::basic_string_view<char>
error: ‘string_view’ is not a member of ‘std’
Nils
std::string stringlisttostring(std::list<std::string> strlist) { std::string liststr; liststr.append("["); for (std::list<std::string>::iterator listit = strlist.begin(); listit-- != strlist.end(); listit++) { liststr.append(*listit); if (listit != strlist.end()) liststr.append(", "); } liststr.append("]"); return liststr; } Also why does this sigfault?
Francisco
I included string_view
What compiler are you using? And what version?
Nils
What compiler are you using? And what version?
g++ (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Francisco
Are you sure you included the correct header?
Nils
yes
Nils
#include <string_view>
Francisco
yes
Then std::string_view should work
Nils
It does not
Francisco
It does not
Then you've messed up your installation
Anonymous
std::list<std::string> split(std::string s, char delimiter, int times) { std::string currpart; std::list<std::string> splitted; int timesdone = 0; for (std::string::iterator strit = s.begin(); strit != s.end(); strit++) { currpart.push_back(*strit); if (*strit == delimiter and timesdone < times) { splitted.push_back(currpart); currpart = ""; times++; } } splitted.push_back(currpart); return splitted; } (Or: https://hastebin.com/kulujukula.cpp) Do you think this is okay?
#include <vector> #include <string> #include <string_view> std::vector<std::string> split(std::string_view s, char delimiter, std::vector<std::string>::size_type times = 0) { std::vector<std::string> to_return; decltype(s.size()) start = 0, finish = 0; while ((finish = s.find_first_of(delimiter, start)) != std::string_view::npos) { to_return.emplace_back(s.substr(start, finish - start)); start = finish + 1; if (to_return.size() == times) { break; } } to_return.emplace_back(s.substr(start)); return to_return; } here's how i would do it
Nils
ah
Nils
I changed my code to vecotr now, getting logic error... :-/
Anonymous
ah
fixed
Nils
I changed my code to vecotr now, getting logic error... :-/
https://hastebin.com/guyuronava.cpp Any idea?
Nils
I solved my string_view problem now btw
Francisco
https://hastebin.com/guyuronava.cpp Any idea?
Nobody likes a 170 loc snippet. Send a minimal example that reproduces your problem
Nils
Uhm… 🤔
Nils
Very hard to do
Nils
https://hastebin.com/ufufihaheq.cpp This is just execute and see command, I can't get it any more minimal
Nils
the problem function is execline()
Nils
mainlyx the for loop starting at line 132 but I am not sure
Francisco
Very hard to do
I've reduced a couple thousands of locs to less than 50, you should be able to reduce your 170 to at most 50
Nils
That would take me more time than finding the error myself… Okay I'll try
Nils
Okay I simply make a paste only containing the problematic funcion
Nils
https://hastebin.com/iyewipepez.cpp
Anonymous
https://hastebin.com/ufufihaheq.cpp This is just execute and see command, I can't get it any more minimal
std::vector<std::string>::iterator cmdsplit_it = strsplit(commandstr, ' ', 1).begin(); std::string command = *cmdsplit_it; cmdsplit_it++; line 119: idk if this is something you should do, isn't the vector a temporary that goes out of scope as soon as this line is done?
Francisco
That would take me more time than finding the error myself… Okay I'll try
You're starting to understand the thing. By reducing the code to the bare minimum to reproduce your problem, you begin to find some hints of where the bug might be