使用STL算法将布尔数组元素设置为false? [英] Set boolean array elements to false using STL algorithm?

查看:104
本文介绍了使用STL算法将布尔数组元素设置为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用什么STL算法将数组的所有bool元素设置为false,只需要一行代码?谢谢!

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" < JA ******** @ optusnet.com.au>在消息中写道

news:43 *********************** @ news.optusnet.com.a u ...

|我用什么STL算法将数组的所有bool元素设置为

false

|只需一行代码?谢谢!

|

|


是什么让你相信你需要算法来做到这一点?


#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;

}


返回0;

}


/ *

bool数组大小= 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

布尔矢量,大小= 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

* /

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:43***********************@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
*/


>
是什么让你相信你需要一个算法吗?
What makes you believe that you need an algorithm to do that?




这就是原因。

class Array {

bool arr [10];

public:

Array(){/ * initialise arr * /}

};


如果我想在a中初始化arr,我应该用什么来代替注释

单行代码?



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写道:
Jason Heyes wrote:

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



这就是原因。
类数组{
bool arr [10 ];
公开:
数组(){/ *初始化arr * /}
};

如果我愿意,我该如何代替评论只用一行代码初始化arr?



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



#include <algorithm>

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

Greg


这篇关于使用STL算法将布尔数组元素设置为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆