字符数组和 string类型比较的区别

#include<iostream>
#include<string>
using namespace std; class area{
public:
area(){ cout << "gouzao:" <<endl; }
area(int i){ this->i = i; cout << "gouzao:" << i << endl; }
area(int w, int h){ this->w = w; this->h = h; cout << "gouzao" << endl; }
~area(){ cout << "xigou "<<i << endl; }
int get()
{
return w*h;
}
void set(int w, int h)
{
this->w = w;
this->h = h;
}
private:
int w;
int h;
int i;
}; int main()
{ //char 数组做比较需要 strcmp
//char a[] = "ab";
//char b[] = "ab";
//cout << (a==b) << endl;//不相等,比较的是两个地址 //cout << strcmp(a, b) << endl;
//cout << strcmp(a, "ab") << endl; string a = "ab"; cout << (a == "ab") << endl; //wright return 0; }

赋值比较:

int main()
{ char ch1[10] = "ab";
char ch2[10] = "cd"; //ch1 = ch2; //错误
strcpy(ch1, ch2); string ch3 = "ab";
string ch4 = "cd"; ch3 = ch4; cout << ch3 << endl;
cout << ch4 << endl; return 0; }

字符串赋值:

#include<iostream>
#include<string>
using namespace std; class area{
public:
area(){ cout << "gouzao:" <<endl; }
area(int i){ this->i = i; cout << "gouzao:" << i << endl; }
area(int w, int h){ this->w = w; this->h = h; cout << "gouzao" << endl; }
~area(){ cout << "xigou "<<i << endl; }
int get()
{
return w*h;
}
void set(int w, int h)
{
this->w = w;
this->h = h;
}
private:
int w;
int h;
int i;
}; int main()
{ //char ch1[10] = "ab";
//char ch2[10] = "cd"; ////ch1 = ch2; //错误
//strcpy(ch1, ch2); string ch3 = "ab";
string ch4 = "cd"; ch3 = ch4; ch3.assign(ch4,,); //部分元素赋值给ch3 cout << ch3 << endl;
cout << ch4 << endl; return ; }

字符串合并:

int main()
{ string ch1 = "ab";
string ch2 = "cd"; ch1 = ch1 + ch2; cout << ch1 << endl;
cout << ch2 << endl; return ; }

计算长度:

int main()
{ string ch1 = "ab";
string ch2 = "cd"; ch1 = ch1 + ch2; cout << ch1 << endl;
cout << ch2 << endl; //计算字符长度
cout << strlen(ch1.c_str()) << endl;
cout << ch1.size() << endl;
cout << ch1.length() << endl; return ; }

字符串部分合并:

int main()
{ //char ch3[10] = "ab";
//char ch4[10] = "abcdefg"; //strncat(ch3, ch4, 3); //cout << ch3 << endl; string ch1 = "ab";
string ch2 = "cdefg"; ch1.append(ch2, ,);
cout << ch1 << endl; return ; }

字符串替换:

int main()
{ //char ch3[10] = "ab";
//char ch4[10] = "abcdefg"; //strncpy(ch3, ch4, 3); //cout << ch3 << endl; string ch1 = "ab";
string ch2 = "cdefg";
char ch5[] = "cdefg";
char ch6 = 'A'; //ch1.replace(0,1,ch2, 2,2);
//ch1.replace(0, 1, ch5, 2, 2);//支持char型数组
//ch1.replace(0, 1, 2, ch6);//支持char 字符 cout << ch1 << endl; return ; }

字符串拷贝:

int main()
{ char ch3[] = "abffffff";
char ch4[] = "abcdefg"; memmove(ch3, ch4, ); cout << ch3 << endl; string ch1 = "abcdefghkjklmn";
char ch5[] = "cdefg"; cout << ch5 << endl; ch1.copy(ch5,,); cout << ch5 << endl; return ; }

字符串插入:

int main()
{ string str1 = "abcdefg";
string str2 = "abc"; str1.insert(,str2, ,); cout << str1 << endl; return ; }

字符串删除:

int main()
{ string str1 = "abcdefg";
str1.erase(,); cout << str1 << endl; return ; }

删除字符串:

int main()
{ string str1 = "abcdefg";
str1.erase(,);
cout << str1 << endl; str1.erase();//第二个以后全部删除
cout << str1 << endl; str1.erase();//清空一个字符串
cout << str1 << endl; return ; }

字符串查找:

int main()
{
char ch1[] = "hello world!";
char *p, c = ''; p = strchr(ch1, c);//返回找到的w字符的地址,找不到则返回空指针 if (p)
{
cout << p << endl;
cout << p - ch1 << endl; //计算找到的字符的下标
} return ; }
int main()
{
string str1("abcdefg"); int f = str1.find('b', ); //从第一个字符开始查找 if (f == string::npos)
{
cout << "not find " << endl;
}
cout << f << endl; f = str1.find_first_not_of('b', ); //查找第一个不是b的字符
cout << f << endl; f = str1.find_last_of('b'); //查找最后一个b的位置 cout << (int)string::npos << endl; return ; }

判断字符串是否为空:

int main()
{ string s1 = ""; if (s1.empty())
{
cout << "empty" << endl;
}else{
cout << "not empty" << endl;
}
return ; }

字符串交换:

int main()
{
char ch1[] = "ofru";
char ch2[] = "";
swap(ch1,ch2); cout << ch1 << endl;
cout << ch2 << endl; string str1 = "ab";
string str2 = "";
str1.swap(str2); cout << str1 << endl;
cout << str2 << endl; return ; }

string字符串转char型

int main()
{
string str1 = "abcde"; const char *p;
p = str1.c_str(); cout << p << endl; return ; }

字符串传参:

int get_length(const char *p)
//int get_length(const char p[])
{
int count = ;
while (*p)
{
count++;
p++;
} return count;
} int main()
{ char a[] = "abc";
char *p = "defg"; cout << get_length(a) << endl;
cout << get_length(p) << endl; return ; }

字符串函数返回:

char *get(const char *str)
{
char *p = new char[strlen(p) +];
strcpy(p, str); return p;
} int main()
{ char a[] = "abc";
char *p = get(a); cout << p << endl; char *p2 = get("abc");
cout << p << endl; char *p3 = "abc";
char *p4 = get(p3);
cout << p4 << endl; delete[]p; return ; }

结构体:

#include<iostream>
#include<string>
using namespace std; struct man{
public:
int age;
char *name; }; int main()
{
struct man one={
,
"中国"
}; cout << one.age << endl;
cout <<one.name<< endl; return ; }

结构体与构造函数:

#include<iostream>
#include<string>
using namespace std; struct man{
man(int c_age);
//public:
int age;
char *name;
string name1; }; man::man(int c_age)
{
age = c_age;
} int main()
{
man one(); cout << one.age << endl; return ;
}

结构体赋值:

#include<iostream>
#include<string>
using namespace std; struct man{
int age;
char *name;
string name1; }; int main()
{
man one = {
, "one", "one1"
}; man two = {
, "two", "two1"
}; one = two; cout << one.age << endl;
cout << one.name << endl;
cout << one.name1 << endl; return ;
}

结构体与函数:

#include<iostream>
#include<string>
using namespace std; struct time{
int hour;
int minute;
}; const int perhour = ;
time sum(time t1, time t2); int main()
{ time t1 = {,};
time t2 = { , }; time total = sum(t1,t2); cout << total.hour << endl;
cout << total.minute << endl; return ;
} time sum(time t1, time t2)
{
time total;
total.minute = (t1.minute + t2.minute) % perhour;
total.hour = t1.hour + t2.hour + ((t1.minute + t2.minute) / perhour); return total; }

指针方式返回:

#include<iostream>
#include<string>
using namespace std; struct time{
int hour;
int minute;
}; const int perhour = ;
time *sum(const time &t1, const time &t2); int main()
{
time t1 = {,};
time t2 = { , }; time *total = sum(t1,t2); cout << total->hour << endl;
cout << total->minute << endl; return ;
} time *sum(const time &t1, const time &t2)
{
time *total = new time;
total->minute = (t1.minute + t2.minute) % perhour;
total->hour = t1.hour + t2.hour + ((t1.minute + t2.minute) / perhour);

  delete total;
return total; }

引用方式传递返回:

#include<iostream>
#include<string>
using namespace std; const string &show(const string &str); int main()
{ string str1 = "abc";
string str2 = show(str1); cout << str2 << endl; return ;
} const string &show(const string &str)
{
return str;
}

c++学习-字符串的更多相关文章

  1. .Net程序员之Python基础教程学习----字符串的使用 [Second Day]

         在The FirstDay 里面学习了列表的元组的使用,今天开始学习字符串的使用.字符串的使用主要要掌握,字符串的格式化(C语言中我们应该都知道,Python和C语言差别不大),字符串的基本 ...

  2. 03- Shell脚本学习--字符串和数组

    字符串 字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似: 单双引号的区别: 双 ...

  3. C语言学习 —— 字符串的学习(一)

    这是本人在学习 C语言有关 字符串内容 时的相关笔记 由于本人技术有限,如有错误,还望指正 C语言中数据类型中只有 字符型(char),而 char型 变量一次只能存储一个字符,在日常工作中经常需要定 ...

  4. Day2 Python基础学习——字符串、列表、元组、字典、集合

    Python中文学习大本营:http://www.pythondoc.com/ 一.字符串操作 一.用途:名字,性格,地址 name = 'wzs' #name = str('wzs')print(i ...

  5. python学习--字符串

    python的字符串类型为str 定义字符串可以用 ‘abc' , "abc", '''abc''' 查看str的帮助 在python提示符里 help(str) python基于 ...

  6. Swift学习—字符串&数组&字典

    字符串 OC和Swift中字符串的区别 在OC中字符串类型时NSString,在Swift中字符串类型是String OC中字符串@"",Swift中字符串"" ...

  7. Java学习----字符串函数

    1.String string类是最终类,不可以有子类 package com.demo.pkg1; public class TestString { public static void main ...

  8. python学习-字符串前面添加u,r,b的含义

    引用:https://www.cnblogs.com/cq90/p/6959567.html u/U:表示unicode字符串 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unico ...

  9. Swift学习字符串、数组、字典

    一.字符串的使用 let wiseWords = "\"I am a handsome\"-boy" var emptyString = "" ...

随机推荐

  1. Git错误non-fast-forward

    Git错误non-fast-forward后的冲突解决 [日期:2012-04-21] 来源:Linux社区  作者:chain2012 [字体:大 中 小]   当要push代码到git时,出现提示 ...

  2. objective-c new关键字

    xxx *a = [xxx new] 等价于 xxx *a = [[xxx alloc]init] ,但如果类的构造函数带参数就不能使用new了. 练习了下<Objective-C 基础教程&g ...

  3. Java——java多态

     /* * 多态: * 成员的特点: * 1.成员变量. *  编译时:参开引用型变量所属类中的是否有调用的成员变量,  有:编译通过,  没有  编译失败. *  运行时: 参考引用变量所属的类 ...

  4. java的nio之:java的bio流下实现的socket服务器同步阻塞模型和socket的伪异步的socket服务器的通信模型

    同步I/O模型的弊端===>每一个线程的创建都会消耗服务端内存,当大量请求进来,会耗尽内存,导致服务宕机 伪异步I/O的弊端分析===>当对Socket的输入流进行读取操作的时候,它会一直 ...

  5. MySQL中行列转换的SQL技巧

    行列转换常见场景 由于很多业务表因为历史原因或者性能原因,都使用了违反第一范式的设计模式.即同一个列中存储了多个属性值(具体结构见下表). 这种模式下,应用常常需要将这个列依据分隔符进行分割,并得到列 ...

  6. centos7下环境配置

    1:  安装memcached 问题:error: libevent is required. If it's already installed, specify its path using –w ...

  7. Jquery遍历元素

    页面中所有的checkBox遍历: <script type="text/javascript" src="/jquery/jquery.js">& ...

  8. Amazon后台登陆以及跟卖

    亚马逊模拟登陆,这里使用的是selenium来登陆,并判断是否登陆成功,以及是否有验证码,并破解验证码登陆. 跟卖主要解决的难题是selenium的新窗口弹出问题,在 # 点击“出售您的” brows ...

  9. webqq协议请求交互过程

    1.http://my.oschina.net/ij2ee/blog/191692 2.http://www.qqxieyi.com/fenxi_show.asp?id=34

  10. R(四): R开发实例-map分布图

    前几章对R语言的运行原理.基本语法.数据类型.环境部署等基础知识作了简单介绍,本节将结合具体案例进行验证测试. 案例场景:从互联网下载全国三甲医院数据,以地图作为背景,展现各医院在地图上的分布图.全国 ...