常用的operator classes
2015-06-09 10:35
357 查看
[cpp] view
plaincopyprint?
void test_plus()
{
// 数组与数组之间求和
int first[] = {1, 2, 3, 4, 5};
int second[] = {10, 20, 30, 40, 50};
int results[5];
transform(first, first + 5, second, results, std::plus<int>());
std::copy(first, first + 5, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
// 数组累计求和
int i[] = {1, 2, 3, 4, 5, 6};
int sum(0);
sum = accumulate(i, i + 6, 0, std::plus<int>());
std::cout << "The result of 1 + 2 + 3 + 4 + 5 + 6 = " << sum << std::endl;
}
void test_minus()
{
int numbers[] = {10, 20, 30};
int result;
result = accumulate(numbers, numbers + 3, 100, std::minus<int>());
std::cout << "The result of 100 - 10 - 20 - 30 = " << result << std::endl;
}
void test_multi()
{
int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int result;
result = accumulate(numbers, numbers + 10, 1, std::multiplies<int>());
std::cout << "The result of 1*2*3*4*5*6*7*8*9*10 = " << result << std::endl;
// 可以做成阶乘
int factorials[10];
partial_sum(numbers, numbers + 10, factorials, std::multiplies<int>());
std::copy(factorials, factorials + 10, std::ostream_iterator<int>(std::cout, "\n"));
// for (int i = 0; i < 10; ++i)
// std::cout << numbers[i] << "! is " << factorials[i] << std::endl;
}
void test_divides()
{
int first[] = {10, 40, 90, 40, 10};
int second[] = {1, 2, 3, 4, 5};
int results[5];
transform(first, first + 5, second, results, std::divides<int>());
std::copy(results, results + 5, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
void test_greater()
{
int numbers[] = {10, 40, 90, 50, 20};
std::vector<int> v(numbers, numbers + 5);
std::sort(v.begin(), v.end(), std::greater<int>());
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
}
// logical_and, logical_or, logical_not应用基本类似.
void test_logical_and()
{
bool foo[] = {true, false, true, false};
bool bar[] = {true, true, false, false};
bool result[4];
transform(foo, foo+4, bar, result, std::logical_and<bool>());
std::cout << std::boolalpha << "Logical AND:\n";
for (int i = 0; i < 4; ++i)
std::cout << foo[i] << " AND " << bar[i] << " = " << result[i] << std::endl;
}
plaincopyprint?
void test_plus()
{
// 数组与数组之间求和
int first[] = {1, 2, 3, 4, 5};
int second[] = {10, 20, 30, 40, 50};
int results[5];
transform(first, first + 5, second, results, std::plus<int>());
std::copy(first, first + 5, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
// 数组累计求和
int i[] = {1, 2, 3, 4, 5, 6};
int sum(0);
sum = accumulate(i, i + 6, 0, std::plus<int>());
std::cout << "The result of 1 + 2 + 3 + 4 + 5 + 6 = " << sum << std::endl;
}
void test_minus()
{
int numbers[] = {10, 20, 30};
int result;
result = accumulate(numbers, numbers + 3, 100, std::minus<int>());
std::cout << "The result of 100 - 10 - 20 - 30 = " << result << std::endl;
}
void test_multi()
{
int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int result;
result = accumulate(numbers, numbers + 10, 1, std::multiplies<int>());
std::cout << "The result of 1*2*3*4*5*6*7*8*9*10 = " << result << std::endl;
// 可以做成阶乘
int factorials[10];
partial_sum(numbers, numbers + 10, factorials, std::multiplies<int>());
std::copy(factorials, factorials + 10, std::ostream_iterator<int>(std::cout, "\n"));
// for (int i = 0; i < 10; ++i)
// std::cout << numbers[i] << "! is " << factorials[i] << std::endl;
}
void test_divides()
{
int first[] = {10, 40, 90, 40, 10};
int second[] = {1, 2, 3, 4, 5};
int results[5];
transform(first, first + 5, second, results, std::divides<int>());
std::copy(results, results + 5, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
void test_greater()
{
int numbers[] = {10, 40, 90, 50, 20};
std::vector<int> v(numbers, numbers + 5);
std::sort(v.begin(), v.end(), std::greater<int>());
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
}
// logical_and, logical_or, logical_not应用基本类似.
void test_logical_and()
{
bool foo[] = {true, false, true, false};
bool bar[] = {true, true, false, false};
bool result[4];
transform(foo, foo+4, bar, result, std::logical_and<bool>());
std::cout << std::boolalpha << "Logical AND:\n";
for (int i = 0; i < 4; ++i)
std::cout << foo[i] << " AND " << bar[i] << " = " << result[i] << std::endl;
}
相关文章推荐
- opencv自适应阀值cvAdaptiveThreshold的应用
- Linux小技巧记录
- Linux chkconfig命令入门
- Shell脚本编程初体验
- CentOS查看系统信息命令和方法
- linux tar.gz zip 解压缩 压缩命令
- 在Linux 中 echo命令 让字体带颜色\033[0-7;30-37;40-47;m string \033[0m
- tomcate 端口号占用,Windows的解决方法
- Tomcat6 配置多虚拟主机,多域名绑定一IP
- Linux-管理文件权限和所有权
- Apache启动提示错误undefined symbol: libiconv_open解决方法
- Ubuntu Qt arm-linux-androideabi-gcc: Command not found
- 动画效果网站
- Install Activemq Cluster on CentOS 6.6
- 每天一个linux命令(52):ifconfig命令
- Linux升级C基本运行库CLIBC
- 系统架构推荐专题文章及书籍-会持续更新
- 11款最棒的Linux数据恢复工具
- ubuntu Linux software install unable to locate package xxx
- Linux下设置export JAVA_OPTS选项进行tomcat JVM内存设置【 linux下tomcat的参数JAVA_OPTS必须设在catalina.sh中cygwin=false前】