What STL algorithm do I use to set all bool elements of an array to false in
just one line of code? Thanks! 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
*/
> 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 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
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() { ... }
> 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.
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
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
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.
> 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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)))
{
|
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";
|
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.
|
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 ?
|
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)
| |
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;
|
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
|
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?
|
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)
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |