« first day (743 days earlier)      last day (2116 days later) » 

6:18 AM
can you guys help find a way to find the correct predecessor in the example I have posted?
 
6:28 AM
hmmm, so if upper is end, and lower is end, then we have no key that matches, and we have no key that's next.
but! if end != begin, then there's a previous key
 
6:49 AM
  std::map<unsigned int, char> m_map = {
    {0, 'a'},
    {4, 'b'},
    {5, 'g'},
    {3, 'y'}
  };
  //print(m_map);

  int keyBegin = -3;
  int keyEnd = 5;
  auto itLow = m_map.lower_bound(keyBegin);// first element >=
  auto itUpp = m_map.upper_bound(keyEnd);// first element >
  bool hasPrev = itLow != m_map.begin() && keyBegin > std::prev(m_map.end())->first;
  bool hasNext = itUpp != m_map.end();

  std::cout << keyBegin << std::endl;
  std::cout << std::prev(m_map.end())->first << std::endl;
-3
5
1
0
how in the world prints true for -3 > 5
std::cout << (keyBegin > std::prev(m_map.end())->first) << std::endl;
on that one
 
classical unsigned with signed comparison surprise
enable warnings and you'll get a warning on that
basically given unsigned x = 5; signed y = -3; a comparison in if(x > y) { ... } will convert y to unsigned instead of converting x to signed
 
thanks! so were saying that if end != begin, then there's a previous key
but it didn't seem to work
wouldn't this be better
 
wait, in which context
 
bool hasPrev = itLow != m_map.begin() && keyBegin > std::prev(m_map.end())->first;
to check that?
hmmm, so if upper is end, and lower is end, then we have no key that matches, and we have no key that's next.
but! if end != begin, then there's a previous key
 
yes
> but it didn't seem to work
I can't see it
the example of where it doesn't work
 
6:55 AM
sorry
bool hasPrev = itLow != m_map.begin() && m_map.begin() != m_map.end();
if you meant that
  std::map<unsigned int, char> m_map = {
    {0, 'a'},
    {4, 'b'},
    {5, 'g'},
    {3, 'y'}
  };
  //print(m_map);

  int keyBegin = -3;
  int keyEnd = 5;
  auto itLow = m_map.lower_bound(keyBegin);// first element >=
  auto itUpp = m_map.upper_bound(keyEnd);// first element >
  bool hasPrev = itLow != m_map.begin() && m_map.begin() != m_map.end();
  bool hasNext = itUpp != m_map.end();

  std::cout << (m_map.begin() == itLow) << "\n";
  std::cout << (m_map.end() == itLow) << "\n";
  std::cout << (m_map.begin() == itUpp) << "\n";
0
1
0
1

Low: 5 g
no next element
 
well, this code is also subject to the signed/unsigned confusion
so first I'd fix that before trying to fix anything else
 
but i never compare anything besides iterators
 
you don't. but std::map does
in fact, m_map.lower_bound(keyBegin); accepts the key
...which has to be converted from int
...and that key is compared quite a lot of times (how it's supposed to know the order otherwise)
Nov 29 '16 at 17:38, by milleniumbug
PSA: Enable warnings in your compiler if you haven't already (in gcc: -Wall -Wextra -pedantic)
 
in order to compare keys or values should I always use the operators > < == etc or the comparators? Like key_comp()
okay
 
@Nerva if you do it with the comparators, you'll naturally tie to the specific container's comparator policy (which can be replaced on individual containers by passing a Compare function object type as a third template parameter)
 
7:18 AM
  std::map<unsigned int, char> m_map = {
    {1, 'a'},
    {4, 'b'},
    {5, 'g'},
    {3, 'y'}
  };

  unsigned int keyBegin = 2;
  unsigned int keyEnd = 4;
  auto itLow = m_map.lower_bound(keyBegin);// first element >=
  auto itUpp = m_map.upper_bound(keyEnd);// first element >
  bool hasPrev = itLow != m_map.begin() && m_map.begin() != m_map.end();
  bool hasNext = itUpp != m_map.end();

  std::cout << (m_map.begin() == itLow) << "\n";
  std::cout << (m_map.end() == itLow) << "\n";
  std::cout << (m_map.begin() == itUpp) << "\n";
seems to work now
 
 
2 hours later…
Ron
9:15 AM
Why is postincrement operator in this sample giving me errors?
Or why is pre-increment so different in this case? I take it the order of expression evaluation makes all the difference. Somewhat counter-intuitive that I can use the first operator but not the second one.
Eeer, found it. Excuse the spam.
 
9:42 AM
hi
can someone please explain me this?
std::vector<myclass>::iterator it = std::find_if(vec.begin(), vec.end(), [var](const myclass & m) -> bool { return m.var == var; });
myclass is a class with "int var" inside
and var is a local integer variable
i don't understand the "const myclass & m" part
i learned a bit lambda expression, but this is the first time i see something like "& m", like c#
 
@Ron yes, postincrement returns rvalue...and you cant initialize a non const lvalue reference with rvalue.
 
10:01 AM
can anyone say how to play loading animation .gif fle while process event using Qt4.8
 
Ron
@FerencRozsa Thanks, appreciate it.
 
@DiCri It's a reference to a const myclass object. See here if references are new to you
 
10:18 AM
yes i know what it is.. i just confused it with "and" operator
and now i'm just having fun with lamdas
 
@DiCri also you can dump a bunch of autos in there
 
i saw this code on a topic, i don't know what's the difference if i use auto or const myclass
 
Also -> bool is redundant
you can use const auto&
 
 
7 hours later…
5:25 PM
hi im having trouble understanding what some parameters are in the strtoul function
unsigned long int strtoul (const char* str, char** endptr, int base);
this is the function
and this is wha they say the parameters do
str
C-string containing the representation of an integral number.
endptr
Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.
base
Numerical base (radix) that determines the valid characters and their interpretation.
If this is 0, the base used is determined by the format in the sequence (see strtol for details).
i dont understand what the endptr and the base parameters do, can someone help me understand?
 
The base parameter is the base of the number system to parse the integer in
We use base 10 in everyday language. Binary is base 2. Octal is base 8. Hexadecimal is base 16
As for what the endptr is: if we have a string: " 42and more text", endptr would point into the str passed in, to just after the last character pased. So in that example, endptr points to the string "and more text"
 
ohhhh ok ok
that makes sence, thanks for explain!
so using 10 insted and 0 is the safest thing to do?
because it will converte str to decimal?
for the base value
 
5:40 PM
Depends on what you want.
If you want base 10, you use 10. If you want to auto-detect the base, you use 0
 
oh ok
 

« first day (743 days earlier)      last day (2116 days later) »