« first day (368 days earlier)      last day (2491 days later) » 

11:14 AM
Hey everyone, does anyone know how I can create a new variable of type const char ** from another char ** variable? This is because I need to pass a non-const to a function that will populate it, but then pass it to another function that only accepts consts.
 
the quick and dirty way is to const_cast
 
When you say dirty, what exactly does that mean, are there better, more efficient ways?
 
nwp
The problem is that the conversion is incorrect. It breaks const-correctness which leads to errors in the program that the compiler cannot catch.
Basically the char ** allows you to modify the char whereas the const char ** allows you to make the const char * point to a thing that cannot be modified. And then stuff breaks.
 
Oh, I see. What if I just want the opposite, non-const into const?
 
Actually that's the exact problem
char** c = fill();
foo(c); //takes const char**
c[0] can now point to const memory but is not marked const
 
11:25 AM
I am trying to write a function to support searching strings with wildcards
I don't know where to start
I want to allow search strings like: "%_abcdef", "asad_%", "asdsd%asdasd"
where % can be anything
I am not allowed to use external libraries
and I'm using C
 
nwp
@Yashas replace % with * and use std::regex or some C equivalent (search for C regex library)
 
I am supposed to write the code :/
 
look up how DFAs work
 
what is DFA?
 
deterministic finite automata
a state machine
for each state you have a transition to another state for each input char
in this case you can easily build the nondeterministic variant (where there are multiple next states per char) and use simultaneous stepping
 
 
4 hours later…
3:29 PM
@Yashas For inspiration: stackoverflow.com/a/3300547/179910
 
 
3 hours later…
6:05 PM
Hey guys, I'm kindof curious about a comment that someone wrote to a Q on SO: stackoverflow.com/questions/45108916/…
The author of said comment wrote: "Usually, the user of your class would supply whatever data you need to initialize the field. There is no way of knowing what type ItemType will be."
I'm new to C++, can someone tell me why the compile couldn't resolve at compile-time both a someClass::someClass(int x) and someClass::someClass(char x) or use someClass::someClass(T x) and then switch on the runtime type with typeid?
 
3 messages moved from Lounge<C++>
@ZephyrPellerin the point is that the OP is trying to initialize the member with some default value, because he wants the class to be default constructible for some unknown reason
and the comment tells him not to do that, and just make his class have a one-arg constructor
 
thanks @milleniumbug
 
that is, a someClass::someClass(ItemType x)
also, a default constructor is the one that can be called with zero arguments
 
humorously enough, he responded with a comment and gave a completely different response, but your answer is more satisfying (and seemingly more correct)
 
so someClass::someClass(T x) wouldn't be a default constructor
 
6:13 PM
right
actually, after some reading, your answer is definitively more correct.
so thank you
 
6:32 PM
Hey
I just discovered R"V0G0N("vdsvdsvdsv "))V0G0N";
Does any1 know if there is a way to use .repalce with it ?
and to use .format() < similar to python format?
I have long text that I'd like to replace few words in as well as to add few words in specific places
 
nwp
6:52 PM
@Dariusz this?
@Dariusz std::string doesn't have that. If you happen to use Qt you can use QString::arg.
 
@nwp that appewar to not be working using raw string literals
 
nwp
@Dariusz It should be. Make an example where it doesn't and put it on ideone or coliru.
(I got to go for a bit)
 
@nwp I will totally miss you xxx
 
7:13 PM
@Dariusz you're looking for string interpolation. No decent function to do in C++ standard
there's snprintf, but it's fairly bad
There's fmtlib and Boost.Format
 
humh, interesting thanks!, I take .format does not exist either?
 
I was referring to .format, yes
 
ah
so there is no .format < python alike
what about repalce?
replace*
 
Replacing occurrences of a string can be done with a combination of std::string::find and std::string::replace
these are annoying to use
 
well when I try to use this from pastebin above I get crash >
mySt = mySt.replace( mySt.begin(), mySt.end(), 'Pros', 'ffff');
 
7:16 PM
well duh
 
or did I do someting incorrectly?
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
 
looking at that statement, it's probably easier to say what's done correctly
 
oh dear
I've updated pastebin but it stil lcrashes
 
a.) all std::string functions operate in-place b.) 'Pros' and 'ffff' are multicharacter literals, a.k.a. nobody ever uses this c.) the replace function replaces what's at indices [pos, pos+length) with a different string
I'm not particularly surprised with you getting confused because the interface of std::string is fairly abysmal
 
humh, I understand what you wrote, But I have no idea what it means o.o
but this function here works > stackoverflow.com/questions/1494399/… feels like cheat tho... is that how its meant to use .replace?
not very good .replace function if so...
 
7:25 PM
10 mins ago, by milleniumbug
these are annoying to use
3 mins ago, by milleniumbug
I'm not particularly surprised with you getting confused because the interface of std::string is fairly abysmal
@Dariusz I know
 
ahh C++ <3... (I wanna go back to python lol), thanks for help :- ) I was not sure if it was meant to be that... "complex" :- )
 
Boost has some functions that are probably easier to use boost.org/doc/libs/1_64_0/doc/html/string_algo.html
but, of course, it's Boost
so some people will go OMG GET IT AWAY FROM ME
personally I don't get near C++ without Boost
 
I'm slowly learning about boost
there is boost.python there isnt there?
I was reading today about extending C++ with python so I can do some function in python for easy use
 
Most Boost libs are of at least acceptable quality
 
why its called boost?
 
7:30 PM
I don't remember
 
is it because some dude called it boost or there is reason behind it? - a deeper meaning?
 
Still, if there's a better non-Boost lib, I'd probably use that one instead of what Boost provides
 
mmm I see
 
for example, there's pybind11 which I've heard is easier to use
 
is boost still being developed?
 
7:31 PM
sure
 
yeah I was reading pybind11 docs today
I didnt understand most of it but I got something lol
 
8:03 PM
hey i am new to the STL and algorithms.
// reverse algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::reverse
#include <vector> // std::vector

int main () {
std::vector<int> myvector;

// set some values:
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9

std::reverse(myvector.begin(),myvector.end()); // 9 8 7 6 5 4 3 2 1

// print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
if i am using "using namespace std" i can replace" std:: " so can i replace for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) with (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++i);?
 
yes, but it's better to not use using namespace std;
I simply qualify everything with std::
1573
Q: Why is "using namespace std" considered bad practice?

akbiggsI've been told by others on numerous occasions that my teacher's advice of exercising using namespace std in code was wrong. Hence, we should use std::cout and std::cin. Why is using namespace std considered bad practice? Is it really that inefficient or risk declaring ambiguous variables (vari...

 
what does this line mean (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++i)?
i am not able to understand vector<int>::iterator it=myvector.begin(); it!=myvector.end()
it's accessing the iterator named it at the beginning of the vector but what's up with the it!=myvector.end?
 
first of all, the loop can be replaced with a range-based for
for(auto& x : myvector) { std::cout << ' ' << x; }
 
can you simplify the above code with range based for?
 
the for(std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) is C++98-ism
even without the range-based for, this can be simplified to for(auto it = myvector.begin(); it != myvector.end(); ++it)
 
8:11 PM
what's the use of it!=myvector.end()?
and *it is pointer to it how is printing out values instead of the reference?
 
checks whether you're at the end of the vector (or rather, you're at past-the-end iterator)
it is an iterator
 
so *it? is used to print out the values in the vecotr?
vector*
 
*it dereferences an iterator
which gets you the value
 
iterator by default uses values instead of the vector address?
 
what
iterator iterates over the vector contents
 
8:14 PM
oh i got it i thought iterator iterates over the "references aka address" rather than values
 
references are not pointers
 
why you used "auto"? for(auto it = myvector.begin(); it != myvector.end(); ++it)?
 
because auto deduces the type
@SaubhagyaSrivastava *it returns a reference to the vector element, which is not the same as returning the address because references are not pointers
 
when i try to compile the for(auto it = myvector.begin(); it != myvector.end(); ++it)
it says it doens't name a type but it's already defined as "auto"
 
Use -std=c++11 or -std=c++14
 
8:21 PM
TDM-GCC 4.9.2
 
that's quite old, but it supports -std=c++11
 
let me try online compilers
std::vector<int>::iterator it so i don't really need this line?
it's by default understood it is iterator in newer versions?
 
basically auto is working for the initializer?
 
auto x = y; declares a variable x of the same type as y has
 
8:28 PM
it becomes type of vector makes sense
 
the vector iterator, yes
std::vector<int>::iterator it = myvector.begin(); and auto it = myvector.begin(); are functionally the same
 
for (int i=1; i<10; ++i) myvector.push_back(i);
cout<<myvector.push_back(int i);
is this correct if i want to print out the for loop?
 
cout<<myvector.push_back(int i); is not valid syntax
 
then how can i print the vector?
 
your code you posted does print the vector contents
 
8:34 PM
the second for loop does
i am talking about the first for loop do i need an iterator to print out stuff?
 
you can also access elements with myvector[i] or myvector.at(i), but the for loop using iterators and range-based for works for any STL container
and indexing only works for random-access containers
 
int i;
// set some values:
for ( i=1; i<10; i++) myvector.push_back(i);
for(auto it = myvector.begin();it != myvector.end();++it)
std::cout << ' ' << *it;
this worked
but cout<<myvector[i] didn't
@milleniumbug Thanks a lot for bearing my doubts and answering them patiently.
 
9:30 PM
Ok got another genius question. Is this the right command to delete file from hdd?

remove(c:filename) ?
 
no
it won't even compile
 
how to delete file from hdd
this is what I got
what am I missing o.o
    remove(location.c_str());
this works
 
remove(c:filename) is not valid syntax
 
yeh well no I mean remove("C:/something")
 
in that case yes
 
10:18 PM
@Dariusz But what you need is boost::format
 
I got away using the replace function
just made sure that my text had proper unique tags to replace
is any1 familiar with json library here?
I'm geting feedback from server that looks like this ["stuff","lol"] that is a string. I'd like to now decode it in to json object... in pyton I would just do obj = json.loads(string) but how can I do it in c++ ?
 
snap
I'm using this lib
 
seems to have an example in the README
// parse explicitly
auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");
 
mmm
hee that works thanks!
How did u know that parse = loads?
I was looking in here for it nlohmann.github.io/json
 
11:17 PM
Also filenames should be std::wstring else your program will crash and burn, because filenames names can have special characters and also their max path length is hard to predict.
 
well, dealing with filenames portably is annoying
 

« first day (368 days earlier)      last day (2491 days later) »