C++ array元素的比较
发布时间:2020-12-16 07:41:20 所属栏目:百科 来源:网络整理
导读:可以用任何比较运算符比较两个数组容器,只要它们有相同的大小,保存的是相同类型的元素,而且这种类型的元素还要支持比较运算符。示例如下: std::arraydouble,4 these {1.0,2.0,3.0,4.0};std::arraydouble,4 those {1.0,4 them {1.0,2.0};if (these == tho
可以用任何比较运算符比较两个数组容器,只要它们有相同的大小,保存的是相同类型的元素,而且这种类型的元素还要支持比较运算符。示例如下: std::array<double,4> these {1.0,2.0,3.0,4.0}; std::array<double,4> those {1.0,4> them {1.0,2.0}; if (these == those) std::cout << "these and those are equal." << std::endl; if (those != them) std::cout << "those and them are not equal."<< std::endl; if (those < them) std::cout << "those are less than them."<< std::endl; if (them > those) std::cout << "them are greater than those." << std::endl;容器被逐元素地比较。对 ==,如果两个数组对应的元素都相等,会返回 true。对于 !=,两个数组中只要有一个元素不相等,就会返回 true。这也是字典中单词排序的根本方式,两个单词中相关联字母的不同决定了单词的顺序。这个代码片段中所有的比较运算都是 true。所以当它们执行时,会输出 4 条消息。 不像标准数组,只要它们存放的是相同类型、相同个数的元素,就可以将一个数组容器赋给另一个。例如: them = those;赋值运算符左边的数组容器的元素,将会被赋值运算符心边的数组容器的元素覆盖。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- Set Programming Guide
- c#固定长度的随机字符串例子
- 测试驱动开发笔记【初学者】
- CWebBrowser2 create失败Debug版
- flex中parentDocument parent owner 实现 drag
- ruby-on-rails – 如何使用Capybara Rspec Match
- quick cocos2d-x 实战:做一个手机横版格斗游戏4
- 使用Oracle的Security External Password Store功
- Heroku正试图安装sqlite3 gem,即使它不在Gemfile
- ruby-on-rails – 如何在rescue_from中呈现500页
热点阅读