string
string的初始化,在C++中字符串是一种数据类型;
1:string的初始化,遍历,字符串连接。
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main(void){
//string的初始化,在C++中字符串是一种数据类型;
string s1 = "abcdefghijk";
string s2("abcdefghijk");
string s3(s2);
string s4 = s1; //调用拷贝构造函数;
string s5(10, 'a');//10个空间中的字符都是'a';
s5 = s1;
cout<<"s3:"<<s3<<endl;
cout<<"s5:"<<s5<<endl;
//string的遍历,重载了[]操作符;
//1、数组方式遍历[]
for(int i = 0; i < s1.length(); i++){
cout<<s1[i]<<" "; //出现错误(下标越界),不会向外面剖出异常,引起程序的中断;
}
cout<<endl;
//2、迭代器
string::iterator it;
for(it = s1.begin(); it != s1.end(); it++){
cout<<*it<<" ";
}
cout<<endl;
//3、函数at()遍历
for(int i = 0; i < s1.length(); i++){
cout<<s1.at(i)<<" "; //会剖出异常,合理的解决下标越界;
}
cout<<endl;
//字符指针和string的转换
//此时,把s1====>char * 把内存首地址给露出来;
printf("s1:%s \n", s1.c_str());
//s1中的内容拷贝到buf中;
char buf[123] = {0};
s1.copy(buf, 2, 0);//n, pos;下标从0开始拷贝2个字符到buf中,不会是C风格的,注意自己加上0结束标志;
cout<<buf<<endl;
//string子符串的连接
s1 = s1 + s2; //直接+就表:字符串的连接;
s1 += s2; //+=也是字符串的连接;
s1.append(s4); //调用方法append()也是字符串的连接;
cout<<s1<<endl;
return 0;
}
运行结果
2:string的查找,替换
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main(void){
//字符串的查找和替换
string s1 = "wbm hello wbm 111 wbm 222 wbm 333";
//1、第一次出现wbm的下标
int index = s1.find("wbm", 0);
cout<<"index :"<<index<<endl;
//2、求wbm每一次出现的数组下标
/* int offindex = s1.find("wbm", 0);
while(offindex != -1){
cout<<"offindex :"<<offindex<<endl;
offindex += strlen("wbm");
offindex = s1.find("wbm", offindex);
}*/
//3、把小写wbm换成大写
int offindex = s1.find("wbm", 0);
while(offindex != -1){
cout<<"offindex :"<<offindex<<endl;
s1.replace(offindex, strlen("wbm"), "WBM"); //从下标offindex开始,删除n个字符,替换为后面的字符;
offindex += strlen("wbm");
offindex = s1.find("wbm", offindex);
}
cout<<"s1:"<<s1<<endl;
string s3 = "aaa bbb ccc";
s3.replace(0, 3, "AAA"); //替换的函数;
cout<<"s3:"<<s3<<endl;
return 0;
}
运行结果:
3:区间的删除和插入
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(void){
//区间删除和插入
string s1 = "hello1 hello2 hell03";
string::iterator it = find(s1.begin(), s1.end(), 'l');
if(it != s1.end()){
s1.erase(it); //删除算法;
}
cout<<"s1 :"<<s1<<endl;
s1.erase(s1.begin(), s1.end()); //删除从pos开始的n个字符;
cout<<"s1全部删除:"<<s1<<endl;
cout<<"s1的长度:"<<s1.length()<<endl;
string s2 = "BBB";
s2.insert(0, "AAA"); //头插法
s2.insert(s2.length(), "CCC");//尾插法
cout<<s2<<endl;
return 0;
}
运行结果:
4:string的大小写转换-->函数指针
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(void){
string s1 = "AAAbbb";
transform(s1.begin(), s1.end(), s1.begin(), 0, toupper);//toupper可以是:函数的入口地址,函数对象,
cout<<s1<<endl;
string s2 = "AAAbbb";
transform(s2.begin(), s2.end(), s2.begin(), 0, tolower);
cout<<s2<<endl;
return 0;
}
array和string的运算符重载
string类
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class MyString{
public:
friend ostream& operator<<(ostream &out, const MyString &s1);
friend istream& operator>>(istream &in, MyString &s2);
MyString(int len = 0){ //默认参数看我们是否自己开辟大小的空间;
if(len != 0){
m_len = len;
m_p = new char[m_len+1];
memset(m_p, 0, m_len);
}else{
m_len = 0;
m_p = new char[m_len+1];
strcpy(m_p, "");
}
}
MyString(const char *p){
if(p == NULL){
m_len = 0;
m_p = new char[m_len+1];
strcpy(m_p, "");
}else{
m_len = strlen(p);
m_p = new char[m_len+1];
strcpy(m_p, p);
}
}
MyString(const MyString &s){
m_len = s.m_len;
m_p = new char[m_len+1];
strcpy(m_p, s.m_p);
}
MyString& operator=(const MyString &t){
if(m_p){
delete []m_p;
m_p = NULL;
m_len = 0;
}
m_len = t.m_len;
m_p = new char[m_len+1];
strcpy(m_p, t.m_p);
return *this;
}
~MyString(){
if(m_p) {
delete []m_p;
m_p = NULL;
m_len = 0;
}
}
public:
MyString operator=(const char *p){
if(m_p){
delete []m_p;
m_p = NULL;
m_len = 0;
}
if(p == NULL){
m_len = 0;
m_p = new char[m_len+1];
strcpy(m_p, "");
}else{
m_len = strlen(p);
m_p = new char[m_len+1];
strcpy(m_p, p);
}
return *this;
}
char& operator[](int index){
return m_p[index];
}
bool operator==(const char *p)const{ //判断与字符串是否相等,看长度和里面的内容是否相等!!!
if(p == NULL){
if(m_len == 0){
return true;
}else{
return false;
}
}else{
if(m_len == strlen(p)){
return !strcmp(m_p, p);
}else{
return false;
}
}
}
bool operator==(const MyString &s)const{
if(m_len != s.m_len){
return false;
}
return !strcmp(m_p, s.m_p);
}
bool operator!=(const char *p)const{
return !(*this == p);
}
bool operator!=(const MyString &s)const{
return !(*this == s);
}
int operator<(const char *p)const{
return strcmp(m_p, p);
}
int operator<(const MyString &s)const{
return strcmp(m_p, s.m_p);
}
int operator>(const char *p)const{
return strcmp(p, m_p);
}
int operator>(const MyString &s)const{
return strcmp(s.m_p, m_p);
}
//怎么样把类的指针露出来.
public:
char *c_str(){
return m_p;
}
const char *c_str2(){
return m_p;
}
int length(){
return m_len;
}
private:
int m_len;
char *m_p;
};
ostream& operator<<(ostream &out, const MyString &s1){
out<<s1.m_p;
return out;
}
istream& operator>>(istream &in, MyString &s2){
in>>s2.m_p;
return in;
}
int main(void){
MyString s1;
MyString s2("s2");
MyString s3 = s2;
MyString s4 = "s444444444444";
s4 = "s22222222222";
s4 = s2;
s4[1] = '3';
printf("%c\n", s4[1]); //测试[]改变值了吗?
cout<<s4<<endl;
if(s2 == "s2"){
cout<<"相等"<<endl;
}else{
cout<<"不相等"<<endl;
}
s3 = "aaa";
int flag = (s3 < "bbb");
if(flag < 0){
cout<<"s3小于bbb"<<endl;
}else{
cout<<"s3大于bbb"<<endl;
}
s3 = "adasf";
strcpy(s3.c_str(), "sga");
cout<<s3<<endl;
MyString s9(100);//默认输入要开辟字符串的空间大小;
cout<<"请输入一个数字 :";
cin>>s9;
cout<<s9<<endl;
return 0;
}
运行结果
array类
#include<iostream>
using namespace std;
class Array{
public:
Array(int count);
Array(const Array &t);
~Array();
public:
void setData(int i, int data);
int getData(int i);
int length();
private:
int len;
int *p;
};
Array::Array(int count){
len = count;
p = new int[len];
}
//有指针,的进行深拷贝;
Array::Array(const Array &t){
len = t.len;
p = new int[len];
for(int i = 0; i < t.len; i++){
p[i] = t.p[i];
}
}
Array::~Array(){
if(p){
delete []p;
p = NULL;
}
}
void Array::setData(int i, int data){
p[i] = data;
}
int Array::getData(int i){
return p[i];
}
int Array::length(){
return len;
}
int main(void){
Array array(10);
int i;
for(i = 0; i < array.length(); i++){
array.setData(i, i);
}
for(i = 0; i < array.length(); i++){
cout<<array.getData(i)<<" ";
}
cout<<endl;
Array array1 = array;
for(i = 0; i < array1.length(); i++){
cout<<array1.getData(i)<<" ";
}
cout<<endl;
return 0;
}
运行结果