string类的常用接口说明

STL六大组件:

容器 

算法

配接器

迭代器

仿函数

空间配置器

温馨提示:只讲常用接口,使用方法说明详见代码注释

目录

一、string类对象的常见构造

二、string类对象的容量操作

三、类对象的访问及遍历操作

四、string类对象的修改操作

五、string类非成员函数

六、汇总代码


一、string类对象的常见构造

#include<iostream>
using namespace std;
#include<string>
//typedef basic_string<char16_t> u16string;

std::string s0("Initial string");

//构造空的string类对象,即空字符串(默认构造)
//default (1)	explicit basic_string(const allocator_type& alloc = allocator_type());
std::string s1;

//用C-string来构造string类对象
//copy(2)    basic_string(const basic_string& str);
std::string s2(s0);

//拷贝构造函数
//copy(2)    basic_string(const basic_string& str);
std::string s2(s0);

void Teststring()
{
	string s1;//构造空的string类对象s1
	string s2("hello sunlang");//用C格式字符串构造string类对象s2
	string s3(s2);//拷贝构造s3
}

二、string类对象的容量操作

//返回字符串有效字符长度
//size_type size() const;
int main()
{
	std::string str("Test string");
	std::cout << "The size of str is" << str.size() << "characters.\n";
	return 0;
}
//检测字符串释放为空串,是返回true,否则返回false
//bool empty() const;
int main()
{
	std::string content;
	std::string line;
	std::cout << "Please introduce a text.Enter an empty line to finish:\n";
	do
	{
		getline(std::cin, line);
		content += line + '\n';
	} while (!line.empty());
	std::cout << "The text you introduced was:\n" << content;
	return 0;
}
//清空有效字符
//void clear();
int main()
{
    char c;
    std::string str;
    std::cout << "Please type some lines of text. Enter a dot (.) to finish:\n";
    do {
        c = std::cin.get();
        str += c;
        if (c == '\n')
        {
            std::cout << str;
            str.clear();
        }
    } while (c != '.');
    return 0;
}
//为字符串预留空间**
//void reserve (size_type n = 0);
int main()
{
    std::string str;

    std::ifstream file("test.txt", std::ios::in | std::ios::ate);
    if (file) {
        std::ifstream::streampos filesize = file.tellg();
        str.reserve(filesize);

        file.seekg(0);
        while (!file.eof())
        {
            str += file.get();
        }
        std::cout << str;
    }
    return 0;
}
//将有效字符的个数改成n个,多出的空间用字符C填充
//void resize (size_type n);
//void resize(size_type n, charT c)
int main()
{
	std::string str("I like to code in C");
	std::cout << str << '\n';

	std::string::size_type sz = str.size();

	str.resize(sz + 2, '+');
	std::cout << str << '\n';

	str.resize(14);
	std::cout << str << '\n';
	return 0;
}

三、类对象的访问及遍历操作

//返回pos位置的字符,const string类对象调用
//reference operator[] (size_type pos);
//const_reference operator[] (size_type pos) const;
int main()
{
	std::string str("Test string");
	for (int i = 0; i < str.length(); ++i)
	{
		std::cout << str[i];
	}
	return 0;
}

四、string类对象的修改操作

//在字符串后面追加字符串str
//string(1)
//basic_string& operator+= (const basic_string& str);
//c - string(2)
//basic_string & operator+= (const charT * s);
//character(3)
//basic_string& operator+= (charT c);
int main()
{
	std::string name("John");
	std::string family("Smith");
	name += "K";         // c-string
	name += family;         // string
	name += '\n';           // character

	std::cout << name;
	return 0;
}
//返回C格式字符串
//const charT* c_str() const;
int main()
{
	std::string str("Please split this sentence into tokens");

	char* cstr = new char[str.length() + 1];
	std::strcpy(cstr, str.c_str());

	// cstr now contains a c-string copy of str

	char* p = std::strtok(cstr, " ");
	while (p != 0)
	{
		std::cout << p << '\n';
		p = strtok(NULL, " ");
	}

	delete[] cstr;
	return 0;
}
//从字符串pos位置开始往后找字符C,返回该字符在字符串中的位置
//string(1)
//size_type find(const basic_string& str, size_type pos = 0) const;
//c - string(2)
//size_type find(const charT * s, size_type pos = 0) const;
//buffer(3)
//size_type find(const charT* s, size_type pos, size_type n) const;
//character(4)
//size_type find(charT c, size_type pos = 0) const;
int main()
{
    std::string str("There are two needles in this haystack with needles.");
    std::string str2("needle");

    // different member versions of find in the same order as above:
    std::string::size_type found = str.find(str2);
    if (found != std::string::npos)
        std::cout << "first 'needle' found at: " << found << '\n';

    found = str.find("needles are small", found + 1, 6);
    if (found != std::string::npos)
        std::cout << "second 'needle' found at: " << found << '\n';

    found = str.find("haystack");
    if (found != std::string::npos)
        std::cout << "'haystack' also found at: " << found << '\n';

    found = str.find('.');
    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");
    std::cout << str << '\n';

    return 0;
}

五、string类非成员函数

//输入运算符重载
//template <class charT, class traits, class Alloc>
//basic_istream<charT, traits>& operator>> (basic_istream<charT, traits>& is,
//	basic_string<charT, traits, Alloc>& str);
int main()
{
	std::string name;

	std::cout << "Please, enter your name: ";
	std::cin >> name;
	std::cout << "Hello, " << name << "!\n";

	return 0;
}
//输出运算符重载
//template <class charT, class traits, class Alloc>
//basic_ostream<charT, traits>& operator<< (basic_ostream<charT, traits>& os,
//	const basic_string<charT, traits, Alloc>& str);
int main()
{
	std::string str = "Hello world!";
	std::cout << str << '\n';
	return 0;
}
//获取一行字符串
//template <class charT, class traits, class Alloc>
//(1)basic_istream<charT, traits>& getline(basic_istream<charT, traits>& is,
//	basic_string<charT, traits, Alloc>& str, charT delim);
//(2)
//template <class charT, class traits, class Alloc>
//basic_istream<charT, traits>& getline(basic_istream<charT, traits>& is,
//	basic_string<charT, traits, Alloc>& str);
int main()
{
	std::string name;

	std::cout << "Please, enter your full name: ";
	std::getline(std::cin, name);
	std::cout << "Hello, " << name << "!\n";

	return 0;
}
//大小比较
int main()
{
	std::string foo = "alpha";
	std::string bar = "beta";

	if (foo == bar) std::cout << "foo and bar are equal\n";
	if (foo != bar) std::cout << "foo and bar are not equal\n";
	if (foo < bar) std::cout << "foo is less than bar\n";
	if (foo > bar) std::cout << "foo is greater than bar\n";
	if (foo <= bar) std::cout << "foo is less than or equal to bar\n";
	if (foo >= bar) std::cout << "foo is greater than or equal to bar\n";

	return 0;
}

六、汇总代码

#include<iostream>
using namespace std;
#include<string>
//测试string容量相关的接口
//size/clear/resize
void Teststring1()
{
	//注意:string类对象支持直接用cin和cout进行输入和输出
	string s("hello,sunlang!!!");//用C-siring来构造string类对象
	cout << s.size() << endl;//返回字符串有效字符长度
	cout << s.length() << endl;//返回字符串有效字符长度
	cout << s.capacity() << endl;//返回空间总大小
	cout << s << endl;
	//将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小
	s.clear();
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	//将s中有效字符个数增加到10个,多出位置用‘a'进行补充
	//"aaaaaaaa"
	s.resize(10, 'a');
	cout << s.size() << endl;
	cout << s << endl;
	cout << s.capacity() << endl;
	//将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行补充
	//"aaaaaaaa\0\0\0\0"
	s.resize(15);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;
	//将s中有效字符个数缩小到5个
	s.resize(5);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;
}
void Teststring2()
{
	string s;
	//测试reserve是否会改变string中有效元素个数
	s.reserve(100);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	//测试reserve参数小于string的底层空间大小时,是否会将空间缩小
	s.reserve(50);
	cout << s.size() << endl;
	cout<<s.capacity()<<endl;
}
//利用reserve提高插入数据的效率,避免增容带来的开销
void TestPushBack()
{
	string s;
	size_t sz = s.capacity();
	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed:" << sz << '\n';
		}
	}
}
//构建vector时,如果提前已经知道string中大概要放多少个元素,可以提前将string中空间设置好
void TestPushBackReserve()
{
	string s;
	s.reserve(100);
	size_t sz = s.capacity();
	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed:" << sz << '\n';
		}
	}

}
//string的遍历
//begin()+end()   for+[]   范围for
//注意:string遍历时使用最多的还是for+下标或者范围for(C++11后才支持)
//begin()+end()大多数使用在需要使用STL提供的算法操作string时,比如:采用reserve逆置string
void Teststring3()
{
	string s1("hello sunlang");
	const string s2("Hello sunlang");
	cout << s1 << " " << s2 << endl;
	cout << s1[0] << " " << s2[0] << endl;
	
	s1[0] = 'H';
	cout << s1 << endl;
	//s2[0]='h';代码编译失败,因为const类型对象不能修改
}
void Teststring4()
{
	string s("hello sunlang");
	//以下三种方式除了遍历string对象,还可以遍历修改string中的字符
	//第一种使用最多
	//for+operator[]
	for (size_t i = 0; i < s.size(); ++i)
	{
		cout << s[i] << endl;
	}
	//迭代器
	string::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it << endl;
		++it;
	}
	//string::reserve_iterator rit =s.rbegin();
	//C++11之后,直接使用auto定义迭代器,让编译器推到迭代器的类型
	//auto rit = s.rbegin();
	//while (rit != s.rend())
	//{
	//	cout << *rit << endl;
	//}
	//范围for
	for (auto ch : s)
	{
		cout << ch << endl;
	}
}
//测试string
//1、插入(拼接)方式:push_back append operator+=
//2、正向和反向查找:find()+rfind()
//3、截取子串:substr()
//4、删除:erase
void Teststring5()
{
	string str;
	str.push_back(' ');//在str后插入空格
	str.append("hello");//在str后追加一个字符"hello"
	str += 's';//在str后追加一个字符'b'
	str += "un";//在str后追加一个字符串"it"
	cout << str << endl;
	cout << str.c_str() << endl;//以C语言的方式打印字符串
	//获取file的后缀
	string file("Test.cpp");
	size_t pos = file.rfind('.');
	string suffix(file.substr(pos, file.size() - pos));
	cout << suffix << endl;
	//npos是string里面的一个静态成员变量
	//static const size_t npos=-1;
	//取出ur1中的域名
	string ur1("https://mp.csdn.net/mp_blog/creation/editor/128085730");
	cout << ur1 << endl;
	size_t start = ur1.find("://");
	if (start == string::npos)
	{
		cout << "invalid url" << endl;
		return;
	}
	start += 3;
	size_t finish = url.find('/', start);
	string address = url.substr(start, finish - start);
	cout << address << endl;
	// 删除 url 的协议前缀
	pos = url.find("://");
	url.erase(0, pos + 3);
	cout << url << endl;
}
int main()
{
	Teststring1();
	Teststring2();
	TestPushBack();
	TestPushBackReserve();
	Teststring3();
	Teststring4();
	Teststring5();
	return 0;
}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
在 Java 中,`Class` 是用于表示一个接口的元数据信息。`Class` 提供了一些常用方法来获取的信息以及进行反射操作。下面是一些常用的 `Class` 方法及其作用: 1. `getName()`:获取的全限定名(包括包名)。 2. `getSimpleName()`:获取的简单名称(不包括包名)。 3. `getPackage()`:获取所在的包。 4. `getSuperclass()`:获取的父。 5. `getInterfaces()`:获取实现的接口列表。 6. `isInterface()`:判断当前是否是接口。 7. `isEnum()`:判断当前是否是枚举型。 8. `isAnnotation()`:判断当前是否是注解型。 9. `isArray()`:判断当前是否是数组型。 10. `isPrimitive()`:判断当前是否是基本数据型。 11. `getDeclaredFields()`:获取的所有字段(包括私有字段)。 12. `getDeclaredMethods()`:获取的所有方法(包括私有方法)。 13. `getDeclaredConstructors()`:获取的所有构造方法(包括私有构造方法)。 14. `newInstance()`:创建的实例对象(需要无参构造方法)。 15. `getDeclaredField(String name)`:获取指定名称的字段。 16. `getDeclaredMethod(String name, Class<?>... parameterTypes)`:获取指定名称和参数型的方法。 这只是一小部分 `Class` 的方法,还有其他一些方法可以用于获取的注解、泛型信息等。通过 `Class` ,我们可以在运行时获取和操作的信息,实现动态性和灵活性。 需要注意的是,`Class` 的方法在 Java 9 中进行了一些调整和新增,具体使用时请查阅相关文档以及适用你使用的 Java 版本。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

发烧的CPU

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

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

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

打赏作者

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

抵扣说明:

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

余额充值