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 = "" ...
随机推荐
- HDU-5781 ATM Mechine(概率DP)
题目大意:某个未知整数x等概率的分布在[0,k]中.每次你都可以从这个整数中减去一个任意整数y,如果x>=y,那么x=x-y,操作次数累计加1:否则,将会受到一次错误提示.当错误提示超过w次,将 ...
- Qt_Window@Qt Command Prompt从命令行创建工程
#include <QApplication> #include <QLabel> int main(int argc, char *argv[]) { QApplicatio ...
- 用 jQuery 实现表单验证(摘抄)
——选自<锋利的jQuery>(第2版)第5章的例题 5.1.5 表单验证 表单作为 HTML 最重要的一个组成部分,几乎在每个网页上都有体现,例如用户提交信息.用户反馈信息和用户查询信 ...
- 论文笔记之:Heterogeneous Image Features Integration via Multi-Modal Semi-Supervised Learning Model
Heterogeneous Image Features Integration via Multi-Modal Semi-Supervised Learning Model ICCV 2013 本文 ...
- freeswitch 挂断前执行脚本
通道变量名 api_hangup_hook 介绍在挂断时执行指定API命令 示例 <action application="set" data="api_hangu ...
- C++ Language
C++ Language Reference https://msdn.microsoft.com/en-us/library/3bstk3k5(v=vs.120).aspx
- Nginx-/etc/sysctl.conf 参数解释
来自<深入理解Nginx模块开发与架构解析> P9 #表示进程(例如一个worker进程)可能同时打开的最大句柄数,直接限制最大并发连接数 fs. #1代表允许将状态为TIME-WAIT状 ...
- xunsearch增量索引改进版
最近测试了xunserach全文索引程序.xunsearch只有LINUX版,所以想用windows服务器请使用其它全文索引程序.xunsearch本身不像coreseek那样自带增量索引的功能,所以 ...
- C#中的static、readonly与const的比较
C#中有两种常量类型,分别为readonly(运行时常量)与const(编译时常量),本文将就这两种类型的不同特性进行比较并说明各自的适用场景. 工作原理 readonly为运行时常量,程序运 ...
- 在VS 2015 RTM 版中 提示 未能正确加载 NuGetPackage包
在原来的项目中曾经启用了Nuget在编译时还原包功能.这样就会在 *.sln在平行目录生成 一个.Nuget文件夹, 删除了它,就好了. 我分析原因是, VS 2015 使用的是 NugetP ...