第六章C性能.pptVIP

  • 8
  • 0
  • 约2.25万字
  • 约 92页
  • 2017-05-11 发布于重庆
  • 举报
第六章C性能

// 判断字串相等版本三 //================================== #includeiostream #includefstream #includealgorithm using namespace std; //---------------------------------- int main(){ ifstream in(string.txt); for(string s,t; inst; ){ int s1=count(s.begin(), s.end(),1); int t1=count(t.begin(), t.end(),1); cout(s1==t1 s.length()==t.length() ? yes\n : no\n); } }//================================= 上述程序不但在语句上缩减了两行,更重要的是通过代码优化而提高了效率。 根据程序运行的效率,在编程阶段主要有三个阶段: 吃透问题,采用合理的方案设计 采用尽可能有效的算法 采用合理的程序结构,内存布局和代码优化 注意的是,for循环语句的头部“string s,t”的操作:“inst”,它使s和t获得循环中赖以处理的数据,同时其结果本身又是条件判断,实际上充当了循环变量初始化、条件判断和循环迭

文档评论(0)

1亿VIP精品文档

相关文档