-2

I have the following function

std::vector<bool> buildBoolList(node* n)
{
    std::vector<bool> boolList;
    for(int i = 0; i < n->next.size(); i++)
    {
        int ival = atoi(n->next[i]->val.c_str());
        bool b = !!ival;
        boolList.push_back(b);
    }
    return boolList;
}

However every time I run my code boolList is always returned with a size of 0. I've run the debugger to ensure that atoi is returning a valid numeric and I've also ensured that b becomes the appropriate boolean value, however I can't seem to figure out why boolList is not populated.

Damon Swayn
  • 1,326
  • 2
  • 16
  • 36
  • what do you mean whit bool b = !!ival; I mean using '!' twice ? and on an int value ? – Engine Jul 29 '13 at 07:28
  • 2
    @Engine Looks like a mad way to cast the value to `bool`. :) – Mario Jul 29 '13 at 07:29
  • Would you mind sharing an example of what's inside `n`? does it contain an array of strings? What's their value? – Mario Jul 29 '13 at 07:30
  • 2
    @Engine that's the easy way of convert integer to bool. – billz Jul 29 '13 at 07:32
  • 2
    You should provide an [SSCCE](http://sscce.org/). – juanchopanza Jul 29 '13 at 07:35
  • 1
    Is this the exact code you're using or just a modified snippet? Are you saying `boolList` contains some elements in the scope of the function, bu then, upon return you get an empty vector? – Marius Bancila Jul 29 '13 at 07:51
  • It is the exact code, the !! is a tip I found to ensure that your boolean value is either 0 or 1. One of the many solutions I tried. – Damon Swayn Jul 29 '13 at 07:58
  • 1
    @DamonSwayn I'd avoid sites that have tips like that. The way to convert an integer to a `bool` is to use one of the comparison operators, e.g. `ival != 0` (or whatever condition you want). – James Kanze Jul 29 '13 at 08:00
  • Oh look, an [SSCCE](http://ideone.com/YDp1WM). So, it turns out there is no problem with pushing `bool` values into a vector of `bool`. – juanchopanza Jul 29 '13 at 08:04
  • Never use vector of bools, its a lie, Ref.- **Scott Meyers's Item 18** – P0W Jul 29 '13 at 08:22

2 Answers2

0

std::vector<bool> has been a kind of experiment by the C++ committee. It is actually a template specialization that stores the bool values tightly packed in memory: one bit per value.

And since you cannot have a reference to a bit, so you might be seeing the size as 0. Why vector<bool>::reference doesn't return reference to bool?

There's nothing wrong with vector<bool>, except that it isn't equivalent to a vector were T is the integer type equivalent to bool. This only shows in performance (CPUs only access bytes at a time, where in a vector<bool> every element is stored in one bit) and memory access (a reference to a first element of a vector is not equivalent to an array like with any other vector<T>. It is part of the Standard, unfortunately: see section 23.3.7 (C++0x FDIS)

Community
  • 1
  • 1
Saby
  • 718
  • 9
  • 29
  • 1
    That may be good advice, but I don't think it has relevance to this particular problem. – juanchopanza Jul 29 '13 at 07:46
  • http://stackoverflow.com/questions/8399417/why-vectorboolreference-doesnt-return-reference-to-bool – Saby Jul 29 '13 at 07:55
  • Interesting, I'm not quite sure I understand "it is not an STL container" though. – Damon Swayn Jul 29 '13 at 07:59
  • @DamonSwayn :I have modified my comments above, plz refer to the existing stackoverflow thread – Saby Jul 29 '13 at 08:01
  • 2
    @Saby What does the fact that its `operator[]` doesn't return a `bool&` have to do with the results of `size()`? – James Kanze Jul 29 '13 at 08:02
  • There's nothing wrong with vector, except that it isn't equivalent to a vector were T is the integer type equivalent to bool. This only shows in performance (CPUs only access bytes at a time, where in a vector every element is stored in one bit) and memory access (a reference to a first element of a vector is not equivalent to an array like with any other vector. It is part of the Standard, unfortunately: see section 23.3.7 (C++0x FDIS). http://stackoverflow.com/questions/6781985/is-the-use-of-stdvectorbool-objects-in-c-acceptable-or-should-i-use-an-al – Saby Jul 29 '13 at 08:02
  • Interesting, looks like I'll have to rejigger my code. Thanks! – Damon Swayn Jul 29 '13 at 08:02
  • 2
    @Saby I think your last comment hits it on the head. There's nothing wrong with `std::vector` per se, but it _doesn't_ implement the interface `std::vector` fully, which means that it is misnamed. – James Kanze Jul 29 '13 at 08:03
0

I think your problem is with:

for(int i = 0; i < n->next.size(); i++)
{
    int ival = atoi(n->next[i]->val.c_str());

I am almost certain you meant something along the lines of:

for(node* i = n; i->next != NULL; i=i->next)
{
    int ival = atoi(i->val.c_str());
Ofir
  • 8,194
  • 2
  • 29
  • 44
  • next is a vector variable containing a list of children to the parent node, a node can have a variable number of children, so that's not what I meant. – Damon Swayn Jul 29 '13 at 08:30
  • I would have thought like Ofir, too. You should either explain why that code is there - what it has to do with your problem - or trim the example down to something more straight forward. If `std::vector::push_back` doesn't work, then `std::vector buildBoolList() { std::vector boolList; boolList.push_back(true); return boolList; }` would be sufficient. – Algoman Jun 26 '17 at 08:47