for(auto &i:s)、for(auto i:s)在C++中的含义

是一种更简洁的for循环

for(auto &i:s)for(auto i:s) 实现出来的效果等同于

for(auto i=0;i<s,size();i++){
cout<<s[i]<<" ";
}

实例演示

#include <iostream>
#include<string>
using namespace std;
int main() {
	int a[5] = {1, 2, 3, 4, 5};
	cout<<"for(auto i:a)效果:"<<endl;
	for (auto i : a) {
		cout << i << " ";
	}
	cout<<endl;
	 cout<<"for(auto &i:a)效果:"<<endl;
	for (auto &i : a) {
		cout<< i << " ";
	}
	string s="hello world";
		cout<<endl;
		cout<<"遍历string:"<<endl;
	for(auto i:s){
		cout<<i;
	}
	return 0;
}

在这里插入图片描述

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Coming to grips with C++11 and C++14 is more than a matter of familiarizing yourself with the features they introduce (e.g., auto type declarations, move semantics, lambda expressions, and concurrency support). The challenge is learning to use those features effectively—so that your software is correct, efficient, maintainable, and portable. That’s where this practical book comes in. It describes how to write truly great software using C++11 and C++14—i.e. using modern C++. Topics include: The pros and cons of braced initialization, noexcept specifications, perfect forwarding, and smart pointer make functions The relationships among std::move, std::forward, rvalue references, and universal references Techniques for writing clear, correct, effective lambda expressions How std::atomic differs from volatile, how each should be used, and how they relate to C++'s concurrency API How best practices in "old" C++ programming (i.e., C++98) require revision for software development in modern C++ Effective Modern C++ follows the proven guideline-based, example-driven format of Scott Meyers' earlier books, but covers entirely new material. "After I learned the C++ basics, I then learned how to use C++ in production code from Meyer's series of Effective C++ books. Effective Modern C++ is the most important how-to book for advice on key guidelines, styles, and idioms to use modern C++ effectively and well. Don't own it yet? Buy this one. Now". -- Herb Sutter, Chair of ISO C++ Standards Committee and C++ Software Architect at Microsoft
A: A+ Certification 2nd Ed For Dummies.pdf A+ Certification For Dummies 3rd Ed.chm Access 2007 AIO Desk Reference For Dummies.pdf Access 2007 Vba Programming For Dummies Feb 2007.pdf Access Forms & Reports For Dummies.pdf Acne For Dummies.pdf ACT 2007 For Dummies.pdf ACT ! 2005 For Dummies.pdf Adobe Acrobat 6 Pdf For Dummies.pdf Adobe Creative Suite 2 AIO Desk Reference For Dummies.pdf Adobe Creative Suite 3 Web Premium AIO Desk Reference For Dummies.pdf Adobe Creative Suite AIO Desk Reference For Dummies.chm Adobe Illustrator Cs For Dummies (2004).chm Adobe Premiere Elements For Dummies.pdf Adobe Suite AIO Desk Reference For Dummies.chm Advertising 2nd Ed For Dummies.pdf Ajax For Dummies.pdf Algebra II For Dummies.PDF Ancient Egyptians For Dummies.pdf Anger Management For Dummies.pdf ** Anxiety & Depression Workbook For Dummies.pdf AP English Literature & Composition For Dummies.pdf ** Arabic For Dummies.pdf ASP Net 2 0 AIO Desk Reference For Dummies.pdf ASP Net 2 0 Everyday Apps For Dummies.pdf ASP Net 3 5 For Dummies Feb 2008.pdf ** Auto Repair For Dummies.pdf Autocad & Autocad Lt AIO Desk Reference For Dummies.pdf ** Autocad 2004 For Dummies.chm Autocad 2005 For Dummies.pdf Autocad 2007 For Dummies.pdf Autocad 2008 3d Modeling Workbook For Dummies.pdf Autocad 2008 For Dummies.pdf ** B: Bass Guitar For Dummies.pdf BEA Weblogic Server 8 For Dummies.pdf Beagles For Dummies.pdf Beginning Java Programming For Dummies.pdf Beginning Programming 4th Ed For Dummies.chm Beginning Programming For Dummies.pdf Bioinformatics For Dummies 2nd Ed.pdf Bit Torrent For Dummies.pdf Blackberry For Dummies 2006.pdf Blackberry Pearl For Dummies Feb 2007.pdf Blackboard For Dummies.pdf Blocking Spam & Spyware For Dummies.pdf Blogging For Dummies.pdf Blues Guitar For Dummies.pdf Bookkeeping Workbook For Dummies.pdf ** Branding For Dummies.pdf British Military History For Dummies.pdf ** Building A Web Site For Dummies 2nd Ed 2004.pdf Building Confidence For Dummies.pdf Building Flash W
C++智能指针:auto_ptr详解 指针,相信⼤家并不陌⽣。⽆论是我们在进⾏查看内存还是在修改字符串,我们都会⽤到指针。 最常见的情况则是我们使⽤malloc或者new申请到了⼀块内存,然后⽤⼀个指针来保存起来。我们都知道有内存的申请那就必须要对它进⾏ 释放的处理,否则会造成最严重的后果——内存泄漏。⼀个或者两个申请的内存我们或许不会忘记去释放,但是如果成千上万⾏代码,你还 能记得住哪个释放了哪个没有释放吗? ⽽智能指针就是为了解决这个问题⽽产⽣的。最开始智能指针是在boost库的,随着时间发展现在已经成为了C11的特性。(虽然我们本 篇要介绍的最基础的auto_ptr在C++11已经被unique_ptr替代了。。) 智能指针的基本原理 智能指针其实是⼀个类,可以通过将普通指针作为参数传⼊智能指针的构造函数实现绑定。只不过通过运算符重载让它"假装"是⼀个指 针,也可以进⾏解引⽤等操作。既然智能指针是⼀个类,对象都存在于栈上,那么创建出来的对象在出作⽤域的时候(函数或者程序结束)会 ⾃⼰消亡,所以在这个类的析构函数写上delete就可以完成智能的内存回收。 Auto_ptr详解 使⽤时,需要包含头⽂件:memory。 auto_ptr,作为智能指针的始祖,能基本实现我们所期望的功能。⽽且设计简单源码易懂,虽然缺陷众多,但作为了解智能指针的研究对象 还是⼗分合适的。 ⾸先我们先来写⼀个测试类⽤于分析。 #include <iostream> #include <memory> using namespace std; class Test { public: Test(int param = 0) //调⽤时,可以指定是第⼏个对象。默认第0个 { num = param; cout << "Simple:" << num << endl; } ~Test() //析构时会指出第⼏个对象被析构。 { cout << "~Simple:" << num << endl; } void PrintSomething() //输出附加的字符串⽅法。 { cout << "PrintSomething:" << info_extend.c_str() << endl; } int num; //代表这个对象的序号。 string info_extend; //附加的字符串。 }; 接下来,我们通过⼏个测试函数来实验⼀下auto_ptr的基本功能,再来了解⼀下它的弊端。 I.Auto_ptr的基本功能 的基本功能 void TestAutoPtr1() { auto_ptr<Test> my_auto (new Test(1)); //绑定⼀个Test类的新建对象 if(my_auto.get()) //get函数⽤来显式返回它拥有的对象指针,此处判⾮空 { my_auto->PrintSomething(); my_auto.get()->info_extend = "Addition"; //对类内成员进⾏操作 my_auto->PrintSomething(); //看是否成功操作 (*my_auto).info_extend += " other"; //实验解引⽤操作 mt_auto->PrintSomething(); } } 运⾏结果: 我们可以看到在绑定时输出Simple:1,之后也能正常实现Test类的功能,同时my_auto可以通过get⽅法进⾏裸指针赋值以及使⽤*进⾏ 解引⽤操作,与普通指针⽆异。最后函数结束后,在调⽤析构函数的同时auto_ptr的析构函数也将Test类的对象delete掉,我加⼊的内存泄 漏探测⼯具也显⽰No memory leaks detected(没有检查测到内存泄漏). II.Auto_ptr的弊端 的弊端1 void TestAutoPtr2() { auto_ptr<Test> my_auto1 (new Test(1)); if(my_auto1.get()) { (*my_auto1).info_extend = "Test"; //先赋予⼀个初始值作为区别两个指针的标志 auto_ptr<Test> my_auto2; my_auto2 = my_auto1; my_auto2->PrintSomething(); //打印发现成功转移 my_auto1->PringSomething(); //崩溃 } } 在这个程序的最后⼀步会崩溃,罪魁祸⾸就是my_auto2 = my_auto1语句。 autp_ptr对赋值运算符重载的实现是reset(Myptr.release()),即reset和release函数的组合。release会释放所有权,reset则是⽤于接受

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快苏排序OAO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值