c++学习-字符串
字符数组和 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++学习-字符串的更多相关文章
- .Net程序员之Python基础教程学习----字符串的使用 [Second Day]
在The FirstDay 里面学习了列表的元组的使用,今天开始学习字符串的使用.字符串的使用主要要掌握,字符串的格式化(C语言中我们应该都知道,Python和C语言差别不大),字符串的基本 ...
- 03- Shell脚本学习--字符串和数组
字符串 字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似: 单双引号的区别: 双 ...
- C语言学习 —— 字符串的学习(一)
这是本人在学习 C语言有关 字符串内容 时的相关笔记 由于本人技术有限,如有错误,还望指正 C语言中数据类型中只有 字符型(char),而 char型 变量一次只能存储一个字符,在日常工作中经常需要定 ...
- Day2 Python基础学习——字符串、列表、元组、字典、集合
Python中文学习大本营:http://www.pythondoc.com/ 一.字符串操作 一.用途:名字,性格,地址 name = 'wzs' #name = str('wzs')print(i ...
- python学习--字符串
python的字符串类型为str 定义字符串可以用 ‘abc' , "abc", '''abc''' 查看str的帮助 在python提示符里 help(str) python基于 ...
- Swift学习—字符串&数组&字典
字符串 OC和Swift中字符串的区别 在OC中字符串类型时NSString,在Swift中字符串类型是String OC中字符串@"",Swift中字符串"" ...
- Java学习----字符串函数
1.String string类是最终类,不可以有子类 package com.demo.pkg1; public class TestString { public static void main ...
- python学习-字符串前面添加u,r,b的含义
引用:https://www.cnblogs.com/cq90/p/6959567.html u/U:表示unicode字符串 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unico ...
- Swift学习字符串、数组、字典
一.字符串的使用 let wiseWords = "\"I am a handsome\"-boy" var emptyString = "" ...
随机推荐
- CentOS 中PHP开启 GD功能
yum install php-gd 然后重启服务器: service httpd restart
- swift与OC混编高级教程之混编框架的创建和调用
首先创建一个project取个名字叫“MyMixed”,选择iOS-framework&library-cocoa touch framework 然后在里面创建一个SwiftView.swi ...
- C中的基本数据类型和变量
C语言中的数据类型 基本数据类型 1) 整型 (int %d) 2) 字符型 (char %c) 3) 浮点型 %d ①. 单精度浮点型(float) ②. 双精度浮点型(double) 2.指 ...
- 【usaco】patrol
很长时间都没想出来的简单题,看了题解才写出来,还是naive 原题: FJ有个农场,其中有n块土地,由m条边连起来.FJ的养牛场在土地1,在土地n有个新开张的雪糕店.Bessie经常偷偷溜到雪糕店,当 ...
- Linux-某电商网站流量劫持案例分析与思考
[前言] 自腾讯与京东建立了战略合作关系之后,笔者网上购物就首选京东了.某天在家里访问京东首页的时候突然吃惊地发现浏览器突然跳到了第三方网站再回到京东,心里第一个反应就是中木马了. 竟然有这样的事,一 ...
- CentOS6.4-RMAN定时任务备份 on 11GR2
1.rman备份脚本位置: /home/oracle ./scripts/ ./bin -----存放rman脚本 ./log ...
- C#脱离Halcon编程开发环境使用方法
在没有安装Halcon开发程序(HDevelop (SSE2))的电脑上面编程,使C#脱离Halcon编程开发环境使用方法,除了按照Halcon与编程环境必须要做的设置步骤外,还需要做如下两个工作: ...
- postgresql downgrade issue
Q: Dear Support Team, If we use ubuntu server to install postgresql9.4, how can we keep original dat ...
- A Flock Of Tasty Sources On How To Start Learning High Scalability
This is a guest repost by Leandro Moreira. When we usually are interested about scalability we look ...
- Oracle数据库——用户、方案的创建与管理
一.涉及内容 1.掌握用户.方案与权限的基本概念. 2.熟练掌握用户操作的相关命令. 二.具体操作 (一)选择题: 1.关于方案的描述下列哪一项不正确?(C) A.表或索引等对象一定属于某一个方案 B ...