字符串
char s2[100];// c风格字符串 值类型
初始化 一样 =
赋值 strcpy(s2,s3)
连接 strcat(s2,s3)
比较 strcmp(s2,s3)
长度 strlen(s2)
查找 字符 strchr(char *,int) 字符串 strstr(char *,char *)
访问某个元素 s2[i] 一样
string s1; //c++风格字符串 引用类型 需要 #include <string> 头文件
初始化一样 =
赋值 =
连接 +=
比较 == != > >= < <=
长度 s1.size() 或者 s1.length()
查找 s1.find(); 字符或者字符串
访问某个元素 s1[i] 一样
如何沟通?
c风格可以自动转换成string
string s2 = "abcdefg";
if(s2 > "abcdefg"){}
string 转化成 const char * , s1.c_str();
http://www.cppblog.com/lmlf001/archive/2006/04/19/5883.html
bool类型
char s2[100];// c风格字符串 值类型
初始化 一样 =
赋值 strcpy(s2,s3)
连接 strcat(s2,s3)
比较 strcmp(s2,s3)
长度 strlen(s2)
查找 字符 strchr(char *,int) 字符串 strstr(char *,char *)
访问某个元素 s2[i] 一样
string s1; //c++风格字符串 引用类型 需要 #include <string> 头文件
初始化一样 =
赋值 =
连接 +=
比较 == != > >= < <=
长度 s1.size() 或者 s1.length()
查找 s1.find(); 字符或者字符串
访问某个元素 s1[i] 一样
如何沟通?
c风格可以自动转换成string
string s2 = "abcdefg";
if(s2 > "abcdefg"){}
string 转化成 const char * , s1.c_str();
#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;
#include <string>
enum Color{BLACK,RED,WHITE};
union Student
{
int i;
bool sex;
};
int _tmain(int argc, _TCHAR* argv[])
{
//首先测试c风格的字符串的使用
char s1[20] = "nihao,shijie\n";
char* s2 = "hello,world\n";
char s3[20] = {0}; //这样是空字符串 或者 char s3[20] = {'\0'}; //错误的:char s3[20] = {'0'}; 这是字符串0\0
cout << s1 << ' ' << s2 << ' ' << s3 <<endl;
//赋值
strcpy_s(s3,s2); //<string.h> strcpy_s 和 strcpy 用法一样 但当dest空间小于source时会报错 是一种更加安全的做法
cout << s1 << ' ' << s2 << ' ' << s3 <<endl;
//比较 相等0 大1 小-1
cout << "strcmp(s1,s2)" << strcmp(s1,s2) << ' ' << "strcmp(s2,s3)" << strcmp(s2,s3) << endl;
//长度
cout << strlen(s1) << ' ' << strlen(s2) << ' '<< strlen(s3) << endl; //字符串长度
cout << sizeof(s1) << ' ' << sizeof(s2) << ' ' <<sizeof(s3) << endl; //20 4 20 数据结构的大小
//查找
cout << "strchr(s1,'a'):" << strchr(s1,'a') << endl; //返回a所在的地址 打印出来的是ao,shijie\n
cout << "strchr(s1,'a'):" << strstr(s1,"hao") << endl; //返回hao首字母所在地址 打印出来的是hao,shijie\n
//string 类型字符串
//初始化
string s4 = "hello,world";
string s5 = "nihao,shijie";
string s6 = "s6";
//连接
s6 += s5;
cout << s4 << ' ' << s5 << ' ' <<s6 << endl;
s6 = s4+ s5;
cout << s6 << endl;
//比较 看判断语句是否为真
cout << (s4<s5) << endl; //1
cout << ("goodmorning" < s5) << endl; //1
//字符串长度
cout << s4.length() << ' ' <<s4.size() << endl; // 11 11
//string 中的字符串是放置在堆中的,string对象存放在栈中,大小固定
cout << sizeof(s4) << ' ' << sizeof(s5) <<endl; //32 注:linux下为4
//string 与 char数组的转换
//1 自动转换 如 string s4 = "hello,world"; cout << ("goodmorning" < s5) << endl; //1
//2 string.c_str() 返回值时const char* 类型
cout << s4.c_str() << endl;
//访问
cout << s4[1] << endl; //e
s4[1] = 'b' ; //这个在c++中可以 在c#中貌似会报错
cout << s4 << endl;//hbllo,world
cout << s4.find('a') << endl;
//枚举
Color c = WHITE;
cout << c << endl; //2
//联合
Student s;
s.i = 123;
s.sex = false; //只有一个成员能赋值
//赋值后两个成员指向的地址是一样的
cout << s.sex << ' ' <<s.i << endl;//0 0
cout << &(s.sex) << ' ' << &(s.i) << endl;
s.i = 123;
cout << s.sex << ' ' <<s.i << endl;//123 123
cout << sizeof(s) << endl; //4字节
//引用类型
double d = 123.45;
double &e =d;
e = 123.00;
cout << showpoint << d << ' ' << e << endl; //123.000
cout << &d << ' ' << &e << endl;//0012FE78 0012FE78
const double &f = 123.45; //引用常量要加const
const double &x = d + e; //引用临时变量要加const
// ctrl+k c 注释选中
// ctrl+k u 取消注释
//ctrl + k f 自动排版
system("pause");
return 0;
}
string类常用函数
http://www.cppblog.com/lmlf001/archive/2006/04/19/5883.html
c++没有man手册 随意需要查看帮助文档
enum 枚举 ,c里面是类型 ,c++里面不是
#include <iostream>
#include <string>
using namespace std;
enum Course{UNIX,C,CPP,UC,VC};
enum Color{BLACK,RED,GREEN,YELLO,BLUE};
struct Student
{
string name;
Course co;
};
union Unum
{
int a;
double b;
};
int main()
{
//与c种不同的是 自定义的类型现在不用写关键字
//c种是这样 enum Course c ; struct Student s;
Course c;
c = CPP;
int n = CPP;
cout << n << endl;
Color clr = BLUE;
clr = BLACK;
//clr = 4; //错误
cout << clr << endl;
//强制类型转化
clr = (Color)VC;
cout << clr << endl; //4
Student stu;
Unum num;
num.a = 100;
}
bool类型
#include <iostream>
using namespace std;
int main()
{
bool gender = true;
bool sex = false;
//bool 类型占1字节
cout << &gender << endl; //0xbfca762f
cout << &sex << endl; //0xbfca762e
cout << gender << "," << sex << endl; //1 0
//如何输出 true false呢?
cout << boolalpha << gender << "," << sex << endl;
}
引用类型
double & b = c;
本质是指针常量。
double * const b = &c; (其实也不要想得太复杂,就是别名啦)
/*
* 引用 就是起个别名
*/
#include <iostream>
using namespace std;
int main()
{
//引用变量
double d = 123.45;
double &e = d;
e = 3.145;
cout << d << ", " << e << endl; //引用就相当与是一个别名
cout << &e << "," << &d << endl; //0xbfb0fc20,0xbfb0fc20
//如何引用常量?
const double &c = 3.145;
cout << c << endl;
const double &s = d+5;
cout << s << endl;
// int &n = d; 类型不一致 不能通过
}
http://www.cnblogs.com/younes/archive/2009/11/11/1601223.html