- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
private: double rpart; double ipart; }; Complex Complex::operator+(const Complex c){ return Complex(rpart+c.rpart,ipart+c.ipart); } Complex Complex::operator*(const Complex c){ return Complex(rpart*c.rpart-ipart*c.ipart, rpart*c.ipart+ipart*c.rpart); } Complex Complex::operator=(const Complex c){ if(this==c)return *this; rpart=c.rpart; ipart=c.ipart; return *this; } void Complex::Display() { cout(rpart,ipart); } int main(){ int i; vector Complex compvec1,compvec2; vector Complex::iterator compItbegin,compItend; for(i=0;i10;i++) { Complex* comp= new Complex(i*1.0,i*1.0); compvec1.push_back(*comp); } compItbegin=compvec1.begin(); compItend=compvec1.end(); compvec2.insert(compvec2.begin(),compvec1.begin(),compvec1.end()); for(i=0;icompvec2.size();i++) { if( i%5==0 ) coutendl; compvec2[i].Display(); } coutendl; i=0; while (compItbegin!=compItend){ if ( i%5==0) coutendl; compItbegin-Display(); compItbegin++; i++; } coutendl; return 0; } 程序运行结果如下: (0,0)(1,1)(2,2)(3,3)(4,4) (5,5)(6,6)(7,7)(8,8)(9,9) (0,0)(1,1)(2,2)(3,3)(4,4) (5,5)(6,6)(7,7)(8,8)(9,9) 8.4.4 算法 容器解决的是数据的存储问题也就是数据结 构的问题,但是标准容器只定义了很少的基本 操作,这些操作不可能满足用户的要求,所以 需要标准库提供更多的操作。标准库并没有为 每种容器类型都定义实现各种操作的成员函 数,而是定义了一组算法。标准库中的算法也 称为泛型算法,称为算法是因为它们实现共同 的操作;称为泛型是因为它们可以操作在多种 容器类型上,既包括内置类型也包括标准库中 的容器类,还包括用户自定义的与标准库兼容 的容器类型。 例8.7 标准库中的集中排序和搜索算法。 #include iostream #include algorithm #include vector #include iterator using namespace std; bool greater10(int valule); int main() { const int size=10; int a[size]={11,3,7,100,22,9,0,21,8,16}; vectorint v(a,a+size); ostream_iteratorint output(cout, ); coutvector v contains: ; copy(v.begin(),v.end(),output); vectorint::iterator loc; loc=find(v.begin(),v.end(),16); if(loc!=v.end()) cout\n found 16 at location (loc - v.begin()); else cout\n 16 not found ; loc=find(v.begin(),v.end(),100); if(loc!=v.end()) cout\n found 100 at location (loc - v.begin()); else cout\n 100
文档评论(0)