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 = "" ...
随机推荐
- Oracle学习系列1
两个服务必须启动: OracleOraDb10g*TNListener 和 OracleService*** 使用sqlplusw先进行环境的设置 set linesize 300 ; set pag ...
- C++ Unicode SBCS 函数对照表
C++ Unicode SBCS 函数对照表,以备日后查阅 Generic SBCS UNICODE TCHAR char wchar_t _TEOF EOF WEOF _TINT int wint_ ...
- Fortran编译多个文件(转载)
最近需要在Linux系统下编译多个Fortran程序,在网上搜索了一下,但是资料不多,也许因为这个问题比较简单,不值一提,但还是把我知道的写出来,供大家参考: 方法一: 假如现在有两个Fortran程 ...
- Comparison of B-Tree and Hash Indexes
Understanding the B-tree and hash data structures can help predict how different queries perform on ...
- ABBYY将JPEG文件转换成Word文档的方法
日常工作中处理JPEG格式的图像文件时,有时需要转换成Word文档进行编辑,市场上应用而生了很多转换工具,相信不少人听说过OCR(光学字符识别)软件,可以用来转换图像文件,而在OCR软件中, ABBY ...
- sql server ,OVER(PARTITION BY)函数用法,开窗函数,over子句,over开窗函数
https://technet.microsoft.com/zh-cn/library/ms189461(v=sql.105).aspx https://social.msdn.microsoft.c ...
- linux服务之rsync
http://www.cnblogs.com/itech/archive/2010/06/13/1757952.html rsync与mfs好像有点类似,都是传输块的chunk,chunk的 1)软件 ...
- Swagger-UI 基于REST的API测试/文档类插件
现在多数的项目开发中,网站和移动端都需要进行数据交互和对接,这少不了使用REST编写API接口这种场景.例如我目前的工作,移动端交由了另一团队开发,不同开发小组之间就需要以规范和文档作为标准和协作基础 ...
- win10 phpStudy 80端口被占用
原因是win8下系统默认占用80端口,导致apache无法打开.解决方法: 1.以管理员权限运行c:windowssystem32cmd.exe :2.C:WINDOWSsystem32>net ...
- python sys.argv[]
sys.argv[]是用来获取命令行参数的,是一个由该脚本自身路径和其它输入的参数组成的List.sys.argv[0]表示代码本身文件路径. 这里,当我们执行python using_sys.py ...