C++ string 的使用

string 写复制


一个简单的例子,开始时,s2用s1来初始化,在对字符串做任何改变之前s1和s2指定的是同一物理地址,但对s1进行了修改后我们发现地址发生了变化。
这时的s1和s2已经指向了不同的物理空间。

只有当字符串 发生改变时,才创建各自的拷贝,这种实现方式叫做写时复制。


sample code 1

#include <iostream>
#include <string>

using namespace std;


void main()
{
	int i = 0;
    string s1 = "ABCDEFG";
	string s2 = s1;

	if (s1 == s2)
		cout << "s2 is a reference of s1" << endl;
	else
		cout << "s2 is a copy of s1" << endl;

	s1[0] = '0';

	cout << "s1 = " << s1 << endl;
	cout << "s2 = " << s2 << endl;

	if (s1 == s2)
		cout << "s2 is a reference of s1" << endl;
	else
		cout << "s2 is a copy of s1" << endl;
	cin >>i;
}



C++ string 的使用_第1张图片




string 的创建



一个简单的小例子,总结了一些字符串初始化的方式。


sample code 2

#include <iostream>
#include <string>


using namespace std;


void main()
{
	int i = 0;
	char* p = "abcdefg";

	string s1 = "ABCDEFGHIJKLMN";
	string s2("OPQRSTUVWXYZ");
	string s3(s1);
	string s4(s1, 0, 6);   // string, offset, length

	string s5=s2;
	string s6(p);


	cout << "s1 = " << s1 << endl;
	cout << "s2 = " << s2 << endl;
	cout << "s3 = " << s3 << endl;
	cout << "s4 = " << s4 << endl;
	cout << "s5 = " << s5 << endl;
	cout << "s6 = " << s6 << endl;

	cin >> i;
}



C++ string 的使用_第2张图片





string 操作 - 追加 插入和连接


一个简单的小例子 使用 insert 和 append

#include <iostream>
#include <string>

using namespace std;


void main()
{
	int i = 0;
    string s1 = "ABCDEFGHIJKLMN";

	cout << "s1 = " << s1 << "   s1.size() = " << s1.size() << "  s1.capacity() = " << s1.capacity() << endl << endl;

	s1.insert(5, "abcd");

	cout << "s1.insert(5, abcd)" << endl;
	cout << "s1 = " << s1 << "   s1.size() = " << s1.size() << "  s1.capacity() = " << s1.capacity() << endl << endl;


	s1.reserve(128);
	cout << "s1.reserve(128)" << endl;
	cout << "s1 = " << s1 << "   s1.size() = " << s1.size() << "  s1.capacity() = " << s1.capacity() << endl << endl;


	s1.append("xyz");
	cout << "s1.append(xyz)" << endl;
	cout << "s1 = " << s1 << "   s1.size() = " << s1.size() << "  s1.capacity() = " << s1.capacity() << endl << endl;


	cin >> i;
}



C++ string 的使用_第3张图片




string 操作 - 替换和查找


string.replace(int offset, int length, string newstring) : 参数说明

    offset    : 替换字符的起始位置。


    length    : 替换原字符串中,从起始位置开始向后的替换长度
           举例说明:
     string s1 = "AAAA-BBBB-CCCC-DDDD-EEEE";
            s1.replace(5, 4, "######");  结果:  "AAAA- ######-CCCC-DDDD-EEEE";
            s1.replace(5, 6, "######");  结果:  "AAAA- ######CCC-DDDD-EEEE";
 
      // 注意,此时8已经大于"######"的长度了,它会 按照目标字符串的长度进行替换
            s1.replace(5, 8, "######");  结果:   "AAAA-######CCC-DDDD-EEEE";


    newstring : 替换后的内容。


sample code 4

#include <iostream>
#include <string>

using namespace std;


int findandreplace(string oldstring, string targetstring, string newstring)
{
	int i = 0;

	i = oldstring.find(targetstring);
	cout << "oldstring find targetstring = "<< targetstring <<  " pos = " << i << endl << endl;

	if (i == string::npos)
	{
		cout << "didn't find the target string" << endl;
		return -1;
	}

	cout << "replace the target string = " << "targetstring" << "in oldstring = " << oldstring << "wiht newstring = " << newstring << endl << endl;

	oldstring.replace(i, targetstring.size(), newstring);

    cout << "oldstring = " << oldstring << "   oldstring.size() = " << oldstring.size() << "  oldstring.capacity() = " << oldstring.capacity() << endl << endl;
}

void main()
{
	int i = 0;

         string s1 = "AAAA-BBBB-CCCC-DDDD-EEEE";
	cout << "s1 = " << s1 << "   s1.size() = " << s1.size() << "  s1.capacity() = " << s1.capacity() << endl << endl;
	cout << "s1 replace BBBB with ######" << endl << endl;
	s1.replace(5, 4, "######");
	cout << "s1 = " << s1 << "   s1.size() = " << s1.size() << "  s1.capacity() = " << s1.capacity() << endl << endl;


	string s2 = "DDDD";
	i = s1.find(s2);
	cout << "s1 find s2 = "<< s2 <<  " pos = " << i << endl << endl;
	cout << "replace the s2 in s1 wiht ^^^^^^" << endl << endl;
	s1.replace(i, s2.size(), "^^^^^^");
        cout << "s1 = " << s1 << "   s1.size() = " << s1.size() << "  s1.capacity() = " << s1.capacity() << endl << endl;


	cout << "use findandreplace function: can find the target string" << endl;
	findandreplace(s1, "EEEE", "&&&&&&");


	cout << "use findandreplace function: can not find the target string" << endl;
	findandreplace(s1, "ABCD", "&&&&&&");


	cin >> i;
}




字符串的查找


string.find(): 在字符串中从头至尾进行查找,如果找到返回首次匹配的位置。
        string s1 = "AAAA-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
i = s1.find("BBBB");
        "AAAA- BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
返回值: 5



string.find_first_of(): 在字符串中进行查找,返回第一个与目标字符串中任何字符匹配的字符的位置。
        s1 = "AAAA-BCDE-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
i = s1.find_first_of("BACD");
" AAAA-BCDE-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
返回值: 0

        s1 = "AAAA- BCDE-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
i = s1.find_first_of("BBBB");
        "AAAA-BCDE-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
返回值: 5






string.find_last_of(): 在字符串中进行查找,返回最后一个与目标字符串中任何字符匹配的字符的位置。
        s1 = "AAAA-BCDE-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-AAAA";
i = s1.find_last_of("BBDD");
"AAAA-BCDE-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBB B-CCCC-AAAA";
返回值: 48




string.find_first_not_of(): 在字符串中进行查找,返回第一个与目标字符串中任何字符都不匹配的字符的位置。
s1 = "AAAA-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
i = s1.find_first_not_of("ABCD-");
"AAAA-BBBB-CCCC-DDDD- EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
返回值: 20




string.find_last_not_of():  在字符串中进行查找,返回最后一个与目标字符串中任何字符都不匹配的字符的位置。
s1 ="ABCD-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-DCBA-ABCD";
i = s1.find_last_not_of("ABCD-");
"ABCD-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEE E-BBBB-DCBA-ABCD";
返回值: 43




string.rfind(): 在字符串中从尾至头进行查找,如果找到返回首次匹配的位置。
s1 ="ABCD-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-ABCD-DCBA-ABCD";
i = s1.rfind("ABCD");
"ABCD-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-ABCD-DCBA- ABCD"
返回值: 55


sample code 5

#include <iostream>
#include <string>

using namespace std;


void main()
{
	int i = 0;

    string s1 = "AAAA-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
	i = s1.find("BBBB");
	cout << "find() BBBB in string "<< s1 << "  the pos=" << i << endl << endl;

    s1 = "AAAA-BCDE-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
	i = s1.find_first_of("BACD");
	cout << "find_first_of() BACD in string "<< s1 << "  the pos=" << i << endl << endl;

	i = s1.find_first_of("BBBB");
	cout << "find_first_of() BBBB in string "<< s1 << "  the pos=" << i << endl << endl;


    s1 = "AAAA-BCDE-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-AAAA";
	i = s1.find_last_of("BBDD");
	cout << "find_last_of() BBDD in string "<< s1 << "  the pos=" << i << endl << endl;



	s1 = "AAAA-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
	i = s1.find_first_not_of("ABCD-");
	cout << "find_first_not_of() ABCD- in string "<< s1 << "  the pos=" << i << endl << endl;


	s1 ="ABCD-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-DCBA-ABCD";
	i = s1.find_last_not_of("ABCD-");
	cout << "find_last_not_of() ABCD in string "<< s1 << "  the pos=" << i << endl << endl;



	s1 ="ABCD-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-ABCD-DCBA-ABCD";
	i = s1.rfind("ABCD");
	cout << "rfind() ABCD in string "<< s1 << "  the pos=" << i << endl << endl;


	cin >> i;
}



C++ string 的使用_第4张图片




string 操作 - 删除 和 比较


string.erase(int offset, int length)

    offset: 表示删除的起始位置。


    length: 表示删除的长度。


sample code 6

#include <iostream>
#include <string>

using namespace std;


void main()
{
	int i = 0;

    string s1 = "AAAA-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD";
	cout << "s1 = " << s1 << "   s1.size() = " << s1.size() << "  s1.capacity() = " << s1.capacity() << endl << endl;

	cout << "s1.erase(5, 5);" << endl << endl;
	s1.erase(5, 5);
	cout << "s1 = " << s1 << "   s1.size() = " << s1.size() << "  s1.capacity() = " << s1.capacity() << endl << endl;

	cin >> i;
}



C++ string 的使用_第5张图片




字符串的比较


sample code 7


#include <iostream>
#include <string>

using namespace std;


void main()
{
	int i = 0;
	string s1 = "That";
	string s2 = "This";
	char* p1 = "Hello";
	char* p2 = "That";


	cout << "s1 == s2  :  " << (s1 == s2) << endl;
	cout << "s1 != s2  :  " << (s1 != s2) << endl;
	cout << "s1  > s2  :  " << (s1  > s2) << endl;
	cout << "s1 >= s2  :  " << (s1 >= s2) << endl;
	cout << "s1  < s2  :  " << (s1  < s2) << endl;
	cout << "s1 <= s2  :  " << (s1 <= s2) << endl;

	cout << "s1.compare(char* = Hello)  :  " << s1.compare(p1) << endl;
	cout << "s1.compare(char* = That )  :  " << s1.compare(p2) << endl;

	cout << "s1.compare(s2)  :  " << s1.compare(s2) << endl;
	cout << "s1.compare(s1)  :  " << s1.compare(s1) << endl;


	cout <<"             s1 = " << s1 << "   s2 = " << s2 << endl;
	s1.swap(s2);
	cout << "s1.swap(s2)" << "  s1 = " << s1 << "   s2 = " << s2 << endl;


	cin >> i;
}



C++ string 的使用_第6张图片




你可能感兴趣的:(C++ string 的使用)