Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 1st, 2006, 04:55 AM
Jason Heyes
Guest
 
Posts: n/a
Default Set boolean array elements to false using STL algorithm?

What STL algorithm do I use to set all bool elements of an array to false in
just one line of code? Thanks!


  #2  
Old January 1st, 2006, 07:05 AM
Peter_Julian
Guest
 
Posts: n/a
Default Re: Set boolean array elements to false using STL algorithm?


"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:43b75dd1$0$18200$afc38c87@news.optusnet.com.a u...
| 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[i];
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
*/


  #3  
Old January 1st, 2006, 07:15 AM
Jason Heyes
Guest
 
Posts: n/a
Default Re: Set boolean array elements to false using STL algorithm?

>[color=blue]
> What makes you believe that you need an algorithm to do that?
>[/color]

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?


  #4  
Old January 1st, 2006, 08:15 AM
Greg
Guest
 
Posts: n/a
Default Re: Set boolean array elements to false using STL algorithm?

Jason Heyes wrote:[color=blue][color=green]
> >
> > What makes you believe that you need an algorithm to do that?
> >[/color]
>
> 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?[/color]

#include <algorithm>

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

Greg

  #5  
Old January 1st, 2006, 02:45 PM
Maxim Yegorushkin
Guest
 
Posts: n/a
Default Re: Set boolean array elements to false using STL algorithm?


Jason Heyes wrote:[color=blue][color=green]
> >
> > What makes you believe that you need an algorithm to do that?
> >[/color]
>
> Here is why.
>
> class Array {
> bool arr[10];
> public:
> Array() { /* initialise arr */ }
> };[/color]

You can default initialize member arrays using the following syntax:

Array() : arr() { ... }

  #6  
Old January 2nd, 2006, 01:25 AM
Jason Heyes
Guest
 
Posts: n/a
Default Re: Set boolean array elements to false using STL algorithm?

>[color=blue]
> You can default initialize member arrays using the following syntax:
>
> Array() : arr() { ... }
>[/color]

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::ostream_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.


  #7  
Old January 2nd, 2006, 01:55 PM
romain.gaucher@gmail.com
Guest
 
Posts: n/a
Default Re: Set boolean array elements to false using STL algorithm?

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

  #8  
Old January 3rd, 2006, 01:45 PM
Clark S. Cox III
Guest
 
Posts: n/a
Default Re: Set boolean array elements to false using STL algorithm?

On 2006-01-02 08:47:31 -0500, "romain.gaucher@gmail.com"
<romain.gaucher@gmail.com> said:
[color=blue]
> 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[/color]

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
clarkcox3@gmail.com

  #9  
Old January 5th, 2006, 01:35 PM
Maxim Yegorushkin
Guest
 
Posts: n/a
Default Re: Set boolean array elements to false using STL algorithm?


Jason Heyes wrote:[color=blue][color=green]
> >
> > You can default initialize member arrays using the following syntax:
> >
> > Array() : arr() { ... }
> >[/color]
>
> I tried this in a small program and it did not initialise the array.[/color]

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

You are probably using an utterly outdated compiler, such as VC6.
Upgrade to recent one.

  #10  
Old January 14th, 2006, 11:45 PM
Jason Heyes
Guest
 
Posts: n/a
Default Re: Set boolean array elements to false using STL algorithm?

>[color=blue][color=green]
>> 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[/color]
>
> 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;
> }
>[/color]

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


  #11  
Old January 15th, 2006, 12:55 AM
red floyd
Guest
 
Posts: n/a
Default Re: Set boolean array elements to false using STL algorithm?

Jason Heyes wrote:[color=blue][color=green][color=darkred]
>>> 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[/color]
>> 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;
>> }
>>[/color]
>
> There is no default initialisation of integers, boolean, etc, in arrays.
>
>[/color]

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.
  #12  
Old January 16th, 2006, 10:55 AM
Maxim Yegorushkin
Guest
 
Posts: n/a
Default Re: Set boolean array elements to false using STL algorithm?


romain.gaucher@gmail.com wrote:[color=blue]
> 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[/color]

You are mistaken. Refer to §8.5.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles