Set boolean array elements to false using STL algorithm?

Discussion in 'C++' started by Jason Heyes, Jan 1, 2006.

  1. Jason Heyes

    Jason Heyes Guest

    What STL algorithm do I use to set all bool elements of an array to false in
    just one line of code? Thanks!
    Jason Heyes, Jan 1, 2006
    #1
    1. Advertising

  2. Jason Heyes

    Peter_Julian Guest

    "Jason Heyes" <> wrote in message
    news:43b75dd1$0$18200$...
    | What STL algorithm do I use to set all bool elements of an array to
    false in
    | just one line of code? Thanks!
    |
    |

    What makes you believe that you need an algorithm to do that?

    #include <iostream>
    #include <ostream>
    #include <vector>

    int main()
    {
    const sz(10);
    bool ba[sz] = {false};

    std::cout << "bool array with size = " << sz << "\n";
    for(int i = 0; i < sz; ++i)
    {
    std::cout << "ba[" << i << "] = " << ba;
    std::cout << std::endl;
    }

    std::vector<bool> vb(10, false);

    std::cout << "bool vector with size = " << sz << "\n";
    for(int j = 0; j < sz; ++j)
    {
    std::cout << "ba[" << j << "] = " << ba[j];
    std::cout << std::endl;
    }

    return 0;
    }

    /*
    bool array with size = 10
    ba[0] = 0
    ba[1] = 0
    ba[2] = 0
    ba[3] = 0
    ba[4] = 0
    ba[5] = 0
    ba[6] = 0
    ba[7] = 0
    ba[8] = 0
    ba[9] = 0
    bool vector with size = 10
    ba[0] = 0
    ba[1] = 0
    ba[2] = 0
    ba[3] = 0
    ba[4] = 0
    ba[5] = 0
    ba[6] = 0
    ba[7] = 0
    ba[8] = 0
    ba[9] = 0
    */
    Peter_Julian, Jan 1, 2006
    #2
    1. Advertising

  3. Jason Heyes

    Jason Heyes Guest

    >
    > What makes you believe that you need an algorithm to do that?
    >


    Here is why.

    class Array {
    bool arr[10];
    public:
    Array() { /* initialise arr */ }
    };

    What do I put in place of the comment if I want to initialise arr in just a
    single line of code?
    Jason Heyes, Jan 1, 2006
    #3
  4. Jason Heyes

    Greg Guest

    Jason Heyes wrote:
    > >
    > > What makes you believe that you need an algorithm to do that?
    > >

    >
    > Here is why.
    >
    > class Array {
    > bool arr[10];
    > public:
    > Array() { /* initialise arr */ }
    > };
    >
    > What do I put in place of the comment if I want to initialise arr in just a
    > single line of code?


    #include <algorithm>

    class Array
    {
    bool arr[10];
    public:
    Array()
    {
    std::fill_n( arr, sizeof(arr)/sizeof(arr[0]), 0);
    }
    };

    Greg
    Greg, Jan 1, 2006
    #4
  5. Jason Heyes wrote:
    > >
    > > What makes you believe that you need an algorithm to do that?
    > >

    >
    > Here is why.
    >
    > class Array {
    > bool arr[10];
    > public:
    > Array() { /* initialise arr */ }
    > };


    You can default initialize member arrays using the following syntax:

    Array() : arr() { ... }
    Maxim Yegorushkin, Jan 1, 2006
    #5
  6. Jason Heyes

    Jason Heyes Guest

    >
    > You can default initialize member arrays using the following syntax:
    >
    > Array() : arr() { ... }
    >


    I tried this in a small program and it did not initialise the array.

    #include <iostream>
    #include <algorithm>
    #include <iterator>

    class Array
    {
    int arr[10];
    public:
    Array() : arr()
    {
    std::copy(arr, arr + 10, std::eek:stream_iterator<int>(std::cout,
    "\n"));
    }
    };

    int main()
    {
    Array array;
    return 0;
    }

    The output of the program was this.

    -858993460
    -858993460
    -858993460
    -858993460
    -858993460
    -858993460
    -858993460
    -858993460
    -858993460
    -858993460

    Are you sure you can initialise member arrays in the way you have described?

    Jason.
    Jason Heyes, Jan 2, 2006
    #6
  7. Jason Heyes

    Guest

    you cannot !
    there is no std default contruction of integers, boolean etc.
    "arr()" doesn't mean anything here and you have to do a loop
    , Jan 2, 2006
    #7
  8. On 2006-01-02 08:47:31 -0500, ""
    <> said:

    > you cannot !
    > there is no std default contruction of integers, boolean etc.
    > "arr()" doesn't mean anything here and you have to do a loop


    Are you claiming that the following program can print anything other
    than the number zero?

    #include <iostream>
    using namespace std;

    struct Foo
    {
    int i;
    Foo() : i() { cout << i << endl; }
    };

    int main()
    {
    Foo foo;
    }



    --
    Clark S. Cox, III
    Clark S. Cox III, Jan 3, 2006
    #8
  9. Jason Heyes wrote:
    > >
    > > You can default initialize member arrays using the following syntax:
    > >
    > > Array() : arr() { ... }
    > >

    >
    > I tried this in a small program and it did not initialise the array.


    It does, refer to the standard §8.5/5.

    You are probably using an utterly outdated compiler, such as VC6.
    Upgrade to recent one.
    Maxim Yegorushkin, Jan 5, 2006
    #9
  10. Jason Heyes

    Jason Heyes Guest

    >
    >> you cannot !
    >> there is no std default contruction of integers, boolean etc.
    >> "arr()" doesn't mean anything here and you have to do a loop

    >
    > Are you claiming that the following program can print anything other than
    > the number zero?
    >
    > #include <iostream>
    > using namespace std;
    >
    > struct Foo
    > {
    > int i;
    > Foo() : i() { cout << i << endl; }
    > };
    >
    > int main()
    > {
    > Foo foo;
    > }
    >


    There is no default initialisation of integers, boolean, etc, in arrays.
    Jason Heyes, Jan 14, 2006
    #10
  11. Jason Heyes

    red floyd Guest

    Jason Heyes wrote:
    >>> you cannot !
    >>> there is no std default contruction of integers, boolean etc.
    >>> "arr()" doesn't mean anything here and you have to do a loop

    >> Are you claiming that the following program can print anything other than
    >> the number zero?
    >>
    >> #include <iostream>
    >> using namespace std;
    >>
    >> struct Foo
    >> {
    >> int i;
    >> Foo() : i() { cout << i << endl; }
    >> };
    >>
    >> int main()
    >> {
    >> Foo foo;
    >> }
    >>

    >
    > There is no default initialisation of integers, boolean, etc, in arrays.
    >
    >


    The above code should print 0. If you look he uses the default
    constructor for i.

    int x = int();

    *Will* initialize x to 0. Unfortunately, I don't have a copy of the
    Standard in front of me, so I can't quote chapter and verse.
    red floyd, Jan 15, 2006
    #11
  12. wrote:
    > you cannot !
    > there is no std default contruction of integers, boolean etc.
    > "arr()" doesn't mean anything here and you have to do a loop


    You are mistaken. Refer to §8.5.
    Maxim Yegorushkin, Jan 16, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shalini
    Replies:
    2
    Views:
    498
    Brian Genisio
    Jan 9, 2004
  2. Replies:
    4
    Views:
    799
    Daniel T.
    Feb 16, 2006
  3. J Leonard
    Replies:
    4
    Views:
    12,673
    Mark Space
    Jan 19, 2008
  4. Rui Maciel
    Replies:
    2
    Views:
    1,605
    Rui Maciel
    Dec 1, 2009
  5. Metre Meter
    Replies:
    7
    Views:
    371
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page