【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示

这篇具有很好参考价值的文章主要介绍了【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

   
😎博客昵称:博客小梦
😊最喜欢的座右铭:全神贯注的上吧!!!
😊作者简介:一名热爱C/C++,算法等技术、喜爱运动、热爱K歌、敢于追梦的小博主!

😘博主小留言:哈喽!😄各位CSDN的uu们,我是你的博客好友小梦,希望我的文章可以给您带来一定的帮助,话不多说,文章推上!欢迎大家在评论区唠嗑指正,觉得好的话别忘了一键三连哦!😘
【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

前言🙌

    哈喽各位友友们😊,我今天又学到了很多有趣的知识现在迫不及待的想和大家分享一下! 都是精华内容,可不要错过哟!!!😍😍😍

C语言中的字符串

C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可
能还会越界访问。

标准库中的string类

  1. string是表示字符串的字符串类
  2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
  3. string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator> string;
  4. 不能操作多字节或者变长字符的序列。在使用string类时,必须包含#include头文件以及using namespace std;

string 比较常使用的接口

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++
【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++
【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++
【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++
【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++
【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++
【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++
【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++
【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示,【小梦C嘎嘎——启航篇】,c语言,开发语言,c++

对上述函数和其他函数的测试代码演示:

#include<iostream>
#include<string>
using namespace std;


void Teststring1()
{
	string s1;
	string s2("hello bit");
	string s3(s2);
	string s4(s2.begin(), s2. end());
	s1 = "abcdef";
	
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4<< endl;



}

void test_string3()
{
	string s1("hello world");
	string s2 = "hello world";
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;

	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	for (auto ch : s2)
	{
		cout << ch << " ";
	}
	cout << endl;
}


void func(const string & s)
{
	//string::const_iterator it = s.begin();
	//auto it = s.begin();
	//while (it != s.end())
	//{
	//	//*it = 'a';
	//	cout << *it << " ";
	//	it++;
	//}
	//cout << endl;

	//string::const_reverse_iterator rit = s.rbegin();
	/*auto rit = s.rbegin();
	while (rit != s.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;*/

	auto rit = s.rbegin();
	while (rit != s.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
}

void test_string4()
{
	string s1("hello world");
	string s2 = "hello world";
	func(s1);

}


void test_string5()
{
	string s1("hello worldxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyy");

	string s2(s1);
	cout << s2 << endl;

	string s3(s1, 6, 5);
	cout << s3 << endl;

	string s4(s1, 6, 3);
	cout << s4 << endl;

	string s5(s1, 6);
	cout << s5 << endl;

	string s6(s1, 6, s1.size() - 6);
	cout << s6 << endl;

	string s7(10, 'a');
	cout << s7 << endl;

	string s8(++s7.begin(), --s7.end());
	cout << s8 << endl;

	s8 = s7;
	s8 = "xxx";
	s8 = 'y';
}


void test_string6()
{
	string s1("hello world");
	string s2 = "hello world";
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
	s1.clear();
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;






}


void test_string7()
{
	string s;
	/*s.reserve(100);
	for (auto ch : s)
	{
		cout << ch << " ";
	}
	cout << endl;

	s.push_back('x');
	s.push_back('x');
	s.push_back('x');
	s.push_back('x');
	s += 'n';
	s += "bit helloc";
	for (auto ch : s)
	{
		cout << ch;
	}
	cout << endl;*/

	//size_t old = s.capacity();
	//cout << "初始" << s.capacity() << endl;

	//for (size_t i = 0; i < 100; i++)
	//{
	//	s.push_back('x');

	//	if (s.capacity() != old)
	//	{
	//		cout << "扩容:" << s.capacity() << endl;
	//		old = s.capacity();
	//	}
	//}

	//s.reserve(10);
	//cout << s.capacity() << endl;
	s = "hello bit";
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	s.resize(10, 'a');
	cout << s << endl;
	cout << s.size() << endl;
	cout << s.capacity() << endl;

}

void test_string8()
{
	string s = "hello";
	s.append(" bit");
	cout << s << endl;
	string s3 = "hello";
	string s2;
	s2 = s3 + " bit";
	cout << s2 << endl;

	s2 += s2;
	cout << s2 << endl;

	//s += '#';
	//s += "hello";
	//s += ss;
	//cout << s << endl;

	//string ret1 = ss + '#';
	//string ret2 = ss + "hello";
	//cout << ret1 << endl;
	//cout << ret2 << endl;
}

void test_string9()
{
	std::string str("xxxxxxx");
	std::string base = "The quick brown fox jumps over a lazy dog.";

	str.assign(base);
	std::cout << str << '\n';

	str.assign(base, 5, 10);
	std::cout << str << '\n';
}


void test_string10()
{
	// 空格替换为20%
	std::string s2("The quick brown fox jumps over a lazy dog.");
	string s3;
	for (auto ch : s2)
	{
		if (ch != ' ')
		{
			s3 += ch;
		}
		else
		{
			s3 += "%20";
		}
	}

	for (auto ch : s3)
	{
		cout << ch;
	}
	cout << endl;

	printf("s2:%p\n", s2.c_str());
	printf("s3:%p\n", s3.c_str());

	swap(s2, s3);
	//s2.swap(s3);

	printf("s2:%p\n", s2.c_str());
	printf("s3:%p\n", s3.c_str());
}


void test_string11()
{
    string s1("test.cpp.tar.zip");
    //size_t i = s1.find('.');
    size_t i = s1.rfind('.');

    string s2 = s1.substr(i);
    cout << s2 << endl;

    //string s3("https://legacy.cplusplus.com/reference/string/string/rfind/");
    //string s3("ftp://www.baidu.com/?tn=65081411_1_oem_dg");
	string s3("https://leetcode.cn/problems/first-unique-character-in-a-string/submissions/");
    // 协议      https
    // 域名      leetcode.cn
    // 资源名    problems/first-unique-character-in-a-string/submissions/
	string sub1, sub2, sub3;
	i = s3.find(':');
	sub1 = s3.substr(0, i);
	cout << sub1 << endl;

	size_t len  = s3.find('/', i + 3);
	sub2 = s3.substr(i + 3, len - (i + 3));
	cout << sub2 << endl;
	sub3 = s3.substr(len + 1);
	cout << sub3 << endl;
}


void test_string12()
{
	std::string str("Please, replace the vowels in this sentence by asterisks.");
	std::size_t found = str.find_first_not_of("abc");
	while (found != std::string::npos)
	{
		str[found] = '*';
		found = str.find_first_not_of("abcdefg", found + 1);
	}

	std::cout << str << '\n';

	/*std::string str("Please, replace the vowels in this sentence by asterisks.");
	std::size_t found = str.find_first_of("abcd");
	while (found != std::string::npos)
	{
		str[found] = '*';
		found = str.find_first_of("abcd", found + 1);
	}

	std::cout << str << '\n';*/

}


int main()
{

	/*string s1, s2;
	cin >> s1 >> s2;
	cout << s1 << endl;
	cout << s2 << endl;*/


	string str;
	//cin >> str;
	getline(cin, str, '!');
	cout << str;

	/*size_t i = str.rfind(' ');
	if (i != string::npos)
	{
		cout << str.size() - (i + 1) << endl;
	}
	else
	{
		cout << str.size() << endl;
	}*/
	//Teststring1();
	//test_string2();
	//test_string3();
	//test_string4();
	//test_string5();
	//test_string6();
	//test_string7();
	//test_string8();
	//test_string9();
	//test_string11();
	//test_string12();
	return 0;
}

总结撒花💞

   本篇文章旨在分享的是string 使用知识。希望大家通过阅读此文有所收获
   😘如果我写的有什么不好之处,请在文章下方给出你宝贵的意见😊。如果觉得我写的好的话请点个赞赞和关注哦~😘😘😘文章来源地址https://www.toymoban.com/news/detail-656013.html

到了这里,关于【小梦C嘎嘎——启航篇】string介绍以及日常使用的接口演示的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [C/C++]string类常用接口介绍及模拟实现string类

            在C语言中,字符串是以\\\'\\0\\\'结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问。 C++中对于string的定义为: typedef ba

    2024年03月22日
    浏览(68)
  • 【C++】String类基本接口介绍(多看英文文档)

    string目录 目录  如果你很赶时间,那么就直接看我本标题下的内容即可!! 一、STL简介 1.1什么是STL 1.2STL版本 1.3STL六大组件 1.4STL重要性 1.5如何学习STL 二、什么是string??(本质上是一个类) 三、string的类模板(什么?string居然利用了模板??) 三、string的三种构造(拷贝

    2024年02月07日
    浏览(30)
  • 【C++庖丁解牛】实现string容器的增删查改 | string容器的基本接口使用

    🍁你好,我是 RO-BERRY 📗 致力于C、C++、数据结构、TCP/IP、数据库等等一系列知识 🎄感谢你的陪伴与支持 ,故事既有了开头,就要画上一个完美的句号,让我们一起加油 函数名称 功能说明 push_back 在字符串后尾插字符c append 在字符串后追加一个字符串 operator+= (重点) 在字符

    2024年03月14日
    浏览(55)
  • 《string类的使用介绍》

    本文主要介绍string的常见的接口的使用 字符串是 表示字符序列的类 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作单字节字符字符串的设计特性。 string类是使用char(即作为它的字符类型,使用它的默认char_traits和分配器

    2024年02月03日
    浏览(31)
  • C嘎嘎的运算符重载基础教程以及遵守规则【文末赠书三本】

    博主名字:阿玥的小东东 大家一起共进步! 目录 基础概念 优先级和结合性 不会改变用法 在全局范围内重载运算符

    2024年02月03日
    浏览(57)
  • 模板初阶以及string类使用

    1.泛型编程 泛型编程: 编写与类型无关的通用代码 ,是代码复用的一种手段。 模板是泛型编程的基础。 实现加法: 两个函数只有类型上的不同,逻辑完全一致,能不能 写一份表示逻辑的模板,让编译器帮我们生成函数呢? 2.函数模板 模板的原理图 函数模板格式 templatet

    2024年02月14日
    浏览(28)
  • string类的使用方式的介绍

    目录 前言 1.什么是STL 2. STL的版本 3. STL的六大组件 4.STL的缺陷 5.string 5.1 为什么学习string类? 5.1.1 C语言中的字符串 5.2 标准库中的string类 5.3 string类的常用接口的使用 5.3.1 构造函数 5.3.2 string类对象的容量操作 5.3.3 string类对象的访问及遍历操作 5.3.4 string类对象的修改操作

    2024年02月07日
    浏览(30)
  • String.format()方法的使用介绍

    String.format() 方法中的 % 符号用作占位符,用于将值插入字符串中。它用于使用特定值(例如整数、浮点数或字符串)格式化字符串。% 符号后面跟着一个字母,指定要插入的值的类型,例如 %d 表示整数,%s 表示字符串。要插入的值作为参数传递给 String.format() 方法。

    2024年02月16日
    浏览(22)
  • C++:模板初阶以及string类使用

    1.泛型编程 泛型编程: 编写与类型无关的通用代码 ,是代码复用的一种手段。 模板是泛型编程的基础。 实现加法: 两个函数只有类型上的不同,逻辑完全一致,能不能 写一份表示逻辑的模板,让编译器帮我们生成函数呢? 2.函数模板 模板的原理图 函数模板格式 templatet

    2024年02月13日
    浏览(33)
  • String类的学习笔记(上):介绍String类及其常用方法的使用

    本文介绍了Java中用来描述操作字符串的String类,和其一些常用的基本操作方法,字符串的创建输出,字符串对象的比较,字符串查找,字符串的转化,字符串的替换,字符串拆分,字符串截取,和大小写转换,去除左右空格,子字符串包含,学会使用这些方法,能更方便的使用操作字符串~ 前言

    2023年04月23日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包