字符数组和 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. POJ 1043 What's In A Name?(唯一的最大匹配方法)

                                                            What's In A Name? Time Limit: 1000MS   Memor ...

  2. DELPHI相应鼠标滚轮

    在鼠标的MouseWheel事件里写入以下内容 if WheelDelta < 0 then    SendMessage(scrollBox1.Handle, WM_VSCROLL, SB_L ...

  3. C++@子类类型转换为父类类型

    static_cast(*this) to a base class create a temporary copy. class Window { // base class public: vir ...

  4. 配置Apache将自己的电脑做服务器使局域网内的电脑访问自己的主机

    很多的朋友都想把自己的电脑打造为服务器使别人能够访问.比如说你自己写了一网站,只能自己通过localhost访问或127.0.0.1访问.但是怎么让别人的电脑也能访问呢?来看看自己写的网站.现在我来讲 ...

  5. CUDA学习笔记(一)【转】

    CUDA编程中,习惯称CPU为Host,GPU为Device.编程中最开始接触的东西恐怕是并行架构,诸如Grid.Block的区别会让人一头雾水,我所看的书上所讲述的内容比较抽象,对这些概念的内容没有 ...

  6. php(验证网址是否存在)错误

      $ra=get_headers('http://hi.baidu.com'); if($ra[0]==='HTTP/1.1 200 OK'){ echo 'ok'; } 这是错误的,因为有时会返回 ...

  7. Spring MVC静态资源处理——<mvc:resources /> ||<mvc:default-servlet-handler /> 转载

    Spring MVC静态资源处理——<mvc:resources /> ||<mvc:default-servlet-handler /> mvcmvc:resources  ...

  8. 运行setup.js文件

    C:\Windows\System32>wscript.exe setup.js

  9. 【转】object标签和embed标签

    我们现在大部分人做网页,都是直接用DW插入flash,而且DW也是所见即所得,直接生成了相应的flash显示代码.可是我们又有多少人了解这些直接由DW生成的代码呢?其实我接触flash player标 ...

  10. 【转载】springMVC表单校验+全注解

    在这篇文章中,我们将学习如何使用Spring表单标签, 表单验证使用 JSR303 的验证注解,hibernate-validators,提供了使用MessageSource和访问静态资源(如CSS, ...