[c++] string

c++中的string类和普通的字符串是不同的,但是实际上都是字符串,只不过string类默认减少了末尾\0的操作,而普通的char型数组的最后一位一定是这个,当然char s[10] = "hello",最后一位自动加上\0,而string类的size()操作默认是除了\0以外的字符总数。

配合着stack,先来看一个例子:

#include<iostream>
#include<string>
#include<stack>
#include<stdlib.h>
using namespace std;


int main()
{
	stack<string> s;
	char str[100] = "str";
	s.push(str);
	s.push("hello");
	s.push("world!");
	s.pop();
	s.pop();
	cout << s.top() << endl;
	return 0;
}

本例中创造一个string类型的栈(没有char[]型的),str虽然不是string类但是仍然可以进行压栈和出栈的操作,string实际上只是对字符串的封装,但是str没有str.size()操作。

运行结果:

string类可以直接进行赋值, str = "haha",但是c语言中字符串只有在初始化时char s[10] = "haha"可以赋值,不能 s="haha",但是可以用strcpy(s, "haha")来实现赋值操作。

string的初始值不是NULL,所以最好初始化时先进行 = “”空字符串的操作,另外string不能用memset进行清零,push_back可以在string后连上单个字符,pop_back可以出栈,atoi可以对字符串进行变为整数的操作,但这里的字符串必须是c语言中的字符串,string要先进行char gtr = s.c_str()的操作后才能进行atoi的操作:

string的比较大小可以直接str1 - str2或者 str1 > str2这种比较方式,比较的默认方法都是从第一个字母开始按照字母表的排列方式进行比较,直到出现不同的字符或者到达某个字符串的尾部结束,这样我们可以用来对高精度的大数进行比较,当大数都为正数时,长的字符串大;当长度相等时,直接用 > 这种字符串之间的比较方式进行比较即可。

#include<iostream>
#include<stdlib.h>
#include<cstdio>
#include<string>
using namespace std;
int main()
{
	string s = "";
	s.push_back('h');
	s.push_back('e');
	cout << "s: "<< s << endl;
	s.append("llo");
	cout << "s: " << s << endl;
	cout << "s.size(): " << s.size() << endl;
	string::iterator it;
	printf("使用迭代器进行遍历得到的结果为: ");
	for (it = s.begin(); it < s.end(); it++)
		cout << *it;
	cout << endl;
	s.pop_back();
	s.pop_back();
	cout << "执行2次pop_back后的s: " << s << endl;
	s.pop_back();
	s.pop_back();
	s.pop_back();
	s.push_back('1');
	s.push_back('2');
	int i = atoi(s.c_str());
	cout << " s = 12, atoi(s.c_str())后i为 : " << i  << endl;
	cout << "i - 1 = " << i - 1 << endl;
	return 0;
}

输出结果为:

下面是几个常用的string 函数:

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

int main()
{
    string str = "hello";
    //length和size一样的效果
    for(int i = 0; i < str.size(); i++)
        printf("%c", str[i]);
    printf("\n");
    for(string::iterator it = str.begin(); it != str.end(); it++)
        printf("%c", *it);
    cout << endl;
    string str1 = "he";
    string str2 = "llo";
    str1 += str2;
    cout << str1 << endl; //读入输出整个字符串只能用cin 和 cout
    string str3 = "23";
    str3.insert(str3.begin() + 1, str2.begin(), str2.end());//insert(原字符串的迭代器,it2, it3)
    cout << str3 << endl;
    str3  = "12"; //可以重新赋值
    cout << str3 << endl;
    str3.insert(1, "ooo");//insert(pos, string)
    cout << str3 << endl;
    str3.erase(str3.begin(), str3.end() - 1);
    cout << str3 << endl;
    str3.clear();
    cout << str3.size() <<endl;
    str3 = "hello";
    cout <<str3.substr(2, 2) <<endl; //substr(pos, len),返回的是一个字符串
    if(str3.find("ll")!= string::npos)
        cout << "找到!" << endl;
    str3.replace(0, 2, "HE"); //str.replace(pos, len, str2)
    cout <<str3<<endl;
    reverse(str3.begin(), str3.end());
    cout << str3<<endl;
    return 0;
}

运行结果:

#include<cstdio>
#include<iostream>
#include<string>
#include<cctype>
#include<algorithm>//transform函数
using namespace std;



int main()
{
    string s = "aabb";
    //使得s变为大写
    transform(s.begin(), s.end(), s.begin(), ::toupper);
    cout << s << endl;
    transform(s.begin(), s.end(), s.begin(), ::tolower);
    cout << s << endl;
    //判断字符是否是数字cctype中定义
    if(!isdigit(s[2]))
        cout << "no" << endl;
    char ch = toupper('a');
    cout << ch <<endl;
    //比较大小可以直接使用不等符号
    string s1 = "2222";
    string s2 = "1111";
    if(s1 > s2)
        cout << "2222 > 1111" << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值