STL.string(中)

string

迭代器

cpp 复制代码
int main()
{
	//正向迭代器
	string s1("hello world!");
	string::iterator it = s1.begin();;
	while (it != s1.end())
	{
		cout << *it << ' ';
		++it;
	}
	cout << endl;

	//反向迭代器
	string::reverse_iterator reit = s1.rbegin();
	while (reit != s1.rend())
	{
		cout << *reit << ' ';
		++reit;
	}
	cout << endl;

	//正向const迭代器
	string::const_iterator cit = s1.begin();//修饰的是*cit
	while (cit != s1.end())
	{
		cout << *cit << ' ';
		++cit;
	}
	cout << endl;
	
	//反向const迭代器
	auto rcit = s1.rbegin();
	while (rcit != s1.rend())
	{
		cout << *rcit << ' ';
		++rcit;
	}
	cout << endl;
	return 0;
}

任何容器都可以使用迭代器去访问。树形结构,linux结构等。

find

cpp 复制代码
int main ()
{
  std::string str ("There are two needles in this haystack with needles.");
  std::string str2 ("needle");

  std::size_t found = str.find(str2);
  if (found!=std::string::npos)
    std::cout << "first 'needle' found at: " << found << '\n';//查找str2,1

  found=str.find("needles are small",found+1,6);
  if (found!=std::string::npos)
    std::cout << "second 'needle' found at: " << found << '\n';//查找字符串中前六个在第15的位置开始,3

  found=str.find("haystack");//从开始对字符串进行查找,2
  if (found!=std::string::npos)
    std::cout << "'haystack' also found at: " << found << '\n';

  found=str.find('.');//查找字符'.',从头开始查找,4
  if (found!=std::string::npos)
    std::cout << "Period found at: " << found << '\n';

  // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");//从str中的str2的位置开始,str2的字符长度替换为'preposition'
  //string& replace (size_t pos,  size_t len,  const char* s);
  std::cout << str << '\n';

  return 0;
}

swap

substr

cpp 复制代码
int main()
{
	string s1("file.cpp");
	size_t pos = s1.find('.');
	if (pos < string::npos)
	{
		//string suffix = s1.substr(pos, s1.size() - pos);
		string suffix = s1.substr(pos);
		cout << suffix << endl;
	}
	return 0;
}

rfind

cpp 复制代码
int main()
{
	string s1("file.cpp.tar.zip");
	size_t pos = s1.rfind('.');//倒着开始查找
	if (pos < string::npos)
	{
		//string suffix = s1.substr(pos, s1.size() - pos);
		string suffix = s1.substr(pos);
		cout << suffix << endl;
	}
	return 0;
}

find_first_of(用的很少)

cpp 复制代码
int main()
{
    string str("Please, replace the vowels in this sentence by asterisks.");
    string str2("abc");
    size_t found1 = str.find_first_of(str2);//从0开始查找满足str2的位置
    size_t found2 = str.find_first_of("aeiou");//从0开始查找满足"aeiou"的位置
    size_t found3 = str.find_first_of("aeiou", 5, 3);//从5开始查找满足"aeiou"的前三个的位置
    size_t found4 = str.find_first_of("a");//从0开始查找满足'a'的位置
    while (found2 != std::string::npos)
    {
        str[found2] = '*';
        found2 = str.find_first_of("aeiou", found2 + 1);
    }

    cout << str << '\n';
    return 0;
}

find_last_of(用的很少)

跟find_first_of类似,是倒着查找的。

find_first_not_of(用的很少)

这个是和find_first_of反过来的,如果不是里面的字符就会进行替换,是的话就不替换。

相关推荐
码上淘金3 小时前
【Python】Python常用控制结构详解:条件判断、遍历与循环控制
开发语言·python
Brilliant Nemo3 小时前
四、SpringMVC实战:构建高效表述层框架
开发语言·python
虾球xz3 小时前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
fpcc4 小时前
跟我学c++高级篇——模板元编程之十三处理逻辑
c++
格林威5 小时前
Baumer工业相机堡盟工业相机的工业视觉中为什么偏爱“黑白相机”
开发语言·c++·人工智能·数码相机·计算机视觉
橙子199110165 小时前
在 Kotlin 中什么是委托属性,简要说说其使用场景和原理
android·开发语言·kotlin
androidwork5 小时前
Kotlin Android LeakCanary内存泄漏检测实战
android·开发语言·kotlin
学地理的小胖砸5 小时前
【Python 基础语法】
开发语言·python
Dream it possible!6 小时前
LeetCode 热题 100_只出现一次的数字(96_136_简单_C++)(哈希表;哈希集合;排序+遍历;位运算)
c++·leetcode·位运算·哈希表·哈希集合
DanB247 小时前
Java笔记4
java·开发语言·笔记