473,624 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!
Jan 1 '06 #1
11 10280

"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:43******** *************** @news.optusnet. com.au...
| 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<boo l> 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
*/
Jan 1 '06 #2
>
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?
Jan 1 '06 #3
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

Jan 1 '06 #4

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() { ... }

Jan 1 '06 #5
>
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::ostream_it erator<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.
Jan 2 '06 #6
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 '06 #7
On 2006-01-02 08:47:31 -0500, "ro************ @gmail.com"
<ro************ @gmail.com> 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
cl*******@gmail .com

Jan 3 '06 #8

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.

Jan 5 '06 #9
>
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.
Jan 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
3010
by: deko | last post by:
I have a counter file that records page hits - each hit is a UNIX timestamp in the file. But I'm only interested in page hits in the last 365 days. The below code creates an array from the file and counts hits based on interval - day, month or year. $avs = file($viscounter); foreach ($avs as $val) { if($val>(time()-(86400))) {
14
4338
by: Randell D. | last post by:
Folks, Here's my problem: I am createing an array in a file using the reslut of some PHP and MySQL processing. An example of my array would be examlpe="example one"; examlpe="example two"; examlpe="example three";
3
6180
by: Doug Holton | last post by:
I've got a boolean class property that is almost always false. How can I specify that a class property should not be included in the xml serializer output if it is false? I'm using . will never print it if it is true either.
6
4131
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the end is found is less efficient than using sizeof operator , since size of the array is completely determined at compile time. i don't quite understand this. Could anyone explain to me in detail ?
3
4199
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in Fields) { Object Temp = fi.GetValue(Obj); if (Temp.GetType().IsArray)
24
4369
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what we covered was within the last week of the class and all self taught. Our prof gave us an example of a Java method used to remove elements from an array: public void searchProcess() { int outIt=0;
2
4286
by: Speed | last post by:
Could you please tell me which is the most efficient of way writing a BOOLEAN array to a file. Thanks a ton. Speed
5
15065
by: Manticure | last post by:
Suppose I have a boolean array: boolean b = {true, true, false, false, true, true}; I want to convert it to a decimal number like a binary number. I this case the array represents number 110011 in decimal. So how do I convert the array to binary and then integer?
14
12426
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be non-zero. ***** Here is an example of what I need to do: #define YEAR_1 2005 #define YEAR_2 2007 #define YEARS (YEAR_2 - YEAR_1 + 1)
0
8246
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.