【C++Premier迭代器】-创新互联

C++迭代器记录
  • 学习记录
    • 迭代器

成都创新互联公司从2013年创立,是专业互联网技术服务公司,拥有项目成都网站设计、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元浙江做网站,已为上家服务,为浙江各地企业和个人服务,联系电话:13518219792学习记录 迭代器

迭代器是一种比下标更通用的,用于访问数组、容器中元素的机制。

auto b = v.begin(), e = v.end();

举例:将一段字符串的首字母大写(迭代器访问元素)

//使用下标
string s("hello , today")
if(!s.empty())
	s[0] = toupper(s[0]);

//使用迭代器
string s("hello , today")
if(!s.empty() || s.begin()!=s.end()){auto cur = s.begin();
	*cur = toupper(*cur);//解引用与指针类似
}
	

举例:将一段字符串中的第一个单词改为大写的形式(迭代器访问元素plus)

//使用下标
string s("hello , today");
for(int i = 0; i< s.size(); i++) {if (!s.empty() && s[i] != ' ')
		s[i] = toupper(s[i]);
	else
		break;
}
//或者
string s("hello , today");
for (decltype(s.size()) i = 0; i != s.size() && !isspace(s[i]); ++i) //i的数据类型直接改成int也可
	s[i] = toupper(s[i]);
cout<string s("hello , today");
	for (int i = 0; i< s.size() && !s.empty() && s[i] != ' '; i++) {s[i] = toupper(s[i]);
	}
	cout<	string s("hello , today");
	for (auto it = s.begin(); it != s.end() && *it != ' '; ++it)
		*it = toupper(*it);
	cout<

迭代器类型

vector::iterator it;
	string::iterator it1;
	vector::const_iterator it2;
	string::const_iterator it3;
// 迭代器返回常量cbegin和cend
	vectorv;
	auto it = v.begin() //返回类型为iterator
	auto it = v.cbegin(); //返回类型为const_iterator,值不可更改
	

当迭代器指向的对象是类时,比如一个由字符串组成的向量,涉及到解引用与成员访问操作的结合。

(*it).empty() === it->empty()

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


文章名称:【C++Premier迭代器】-创新互联
分享URL:http://pwwzsj.com/article/hepsg.html