cout
#include <iostream>
int main() {
char * name = "zhangSan";
std::cout << "hello" << name << ", welcome!" << std::endl;
std::cout << "hello";
std::cout << "welcome" << std::endl;
std::cout << "hi";
std::cout << std::endl;
std::cout << "hello"
<< "C++"
<< "clion"
<< std::endl;
return 0;
}
cin
#include <iostream>
int main() {
int number1;
std::cout << "请输入:";
std::cin >> number1;
std::cout << number1 << std::endl;
return 0;
}
sizeof
返回类型或变量的长度,单位为字节。
整型
char、short 、int、long、long long INT_MAX 为 int的最大值
从头文件limits.h中可以看到符号常量: SHRT_MAX 为 short的最大值 LONG_MAX 为 long的最大值 LLONG_MAX 为 long long的最大值
#include <iostream>
int main() {
char n_char_max = CHAR_MAX;
short n_short_max = SHRT_MAX;
int n_int_max = INT_MAX;
long n_long_max = LONG_MAX;
long long n_long_long_max = LLONG_MAX;
std::cout << "char is " << sizeof(char) << std::endl;
std::cout << "char is " << sizeof(n_char_max) << std::endl;
std::cout << "char max is " << CHAR_MAX << std::endl;
std::cout << "short is " << sizeof(short) << std::endl;
std::cout << "short is " << sizeof(n_short_max) << std::endl;
std::cout << "short max is " << SHRT_MAX << std::endl;
std::cout << "int is " << sizeof(int) << std::endl;
std::cout << "int is " << sizeof(n_int_max) << std::endl;
std::cout << "int max is " << INT_MAX << std::endl;
std::cout << "long is " << sizeof(long) << std::endl;
std::cout << "long is " << sizeof(n_long_max) << std::endl;
std::cout << "long max is " << LONG_MAX << std::endl;
std::cout << "long long is " << sizeof(long long) << std::endl;
std::cout << "long long is " << sizeof(n_long_long_max) << std::endl;
std::cout << "long long max is " << LLONG_MAX << std::endl;
return 0;
}
无符号类型
增大变量能够存储的最大值, 如short范围为:-32768 - 32767,无符号为 0 - 65535 如char范围为:-128 - 127,无符号为 0 - 255
#include <iostream>
int main() {
short n_short_1 = SHRT_MAX;
unsigned short n_unsigned_short_1 = SHRT_MAX;
std::cout << "short is " << n_short_1 << std::endl;
std::cout << "unsigned short is " << n_unsigned_short_1 << std::endl;
short n_short_2 = SHRT_MAX;
n_short_2 = n_short_2 + 1;
unsigned short n_unsigned_short_2 = SHRT_MAX;
n_unsigned_short_2 = n_unsigned_short_2 + 1;
std::cout << "short is " << n_short_2 << std::endl;
std::cout << "unsigned short is " << n_unsigned_short_2 << std::endl;
short n_short_3 = 0;
n_short_3 = n_short_3 - 1;
unsigned short n_unsigned_short_3 = 0;
n_unsigned_short_3 = n_unsigned_short_3 - 1;
std::cout << "short is " << n_short_3 << std::endl;
std::cout << "unsigned short is " << n_unsigned_short_3 << std::endl;
return 0;
}
10进制、8进制、16进制
第一位是0,表示8进制 第一位是0x或0X,表示16进制
#include <iostream>
int main() {
int n1 = 10;
int n2 = 010;
int n3 = 0x10;
std::cout << "n1 is " << n1 << std::endl;
std::cout << "n2 is " << n2 << std::endl;
std::cout << "n3 is " << n3 << std::endl;
int n4 = 16;
std::cout << std::hex;
std::cout << "以16进制的方式输出:" << n4 << std::endl;
std::cout << std::dec;
std::cout << "以10进制的方式输出:" << n4 << std::endl;
std::cout << std::oct;
std::cout << "以8进制的方式输出:" << n4 << std::endl;
return 0;
}
初始化方式
int n1 = 100;
int n2(200);
int n3 = {300};
int n4{400};
int n5 = {};
int n6 = {600};
char
#include <iostream>
int main() {
char c1 = 'A';
std::cout << c1 << std::endl;
int i1 = c1;
std::cout << i1 << std::endl;
c1 = c1 + 1;
i1 = c1;
std::cout << c1 << std::endl;
std::cout << i1 << std::endl;
return 0;
}
bool
非零即true,就算是负数,也是true
#include <iostream>
int main() {
bool b1 = true;
bool b2 = false;
std::cout << b1 << std::endl;
std::cout << b2 << std::endl;
b1 = 100;
b2 = -100;
std::cout << b1 << std::endl;
std::cout << b2 << std::endl;
b1 = 0;
std::cout << b1 << std::endl;
return 0;
}
int
交换两个变量的值。
#include <iostream>
void numberChange(int & number1, int & number2){
int temp = 0;
temp = number1;
number1 = number2;
number2 = temp;
}
int main() {
int i1 = 100;
int i2 = 200;
int temp = i1;
i1 = i2;
i2 = temp;
std::cout << i1 << std::endl;
std::cout << i2 << std::endl;
int i3 = 10;
int i4 = 20;
numberChange(i3, i4);
std::cout << i3 << std::endl;
std::cout << i4 << std::endl;
return 0;
}
浮点数
float、double
后缀:f/F 、 L
float f1 = 1.1f;
float f2 = 1.1F;
double d1 = 2.1L;
#include <iostream>
int main() {
float f1 = 1.1;
float f2 = 1.1E2;
float f3 = 1.10;
double d1 = 2.1;
double d2 = 2.1e-3;
double d3 = 10.10;
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout << f1 << std::endl;
std::cout << f2 << std::endl;
std::cout << f3 << std::endl;
std::cout << d1 << std::endl;
std::cout << d2 << std::endl;
std::cout << d3 << std::endl;
return 0;
}
类型转换
static_cast<typeName>(values) 强制类型转换运算符
#include <iostream>
int main() {
char c1 = 'A';
int i1 = (int) c1;
int i2 = int (c1);
int i3 = static_cast<int>(c1);
std::cout << i1 << std::endl;
std::cout << i2 << std::endl;
std::cout << i3 << std::endl;
return 0;
}
名称空间
#include <iostream>
int main() {
using std::cout;
using std::endl;
cout << "方式2" << endl;
return 0;
}
自定义命名空间
#include <iostream>
namespace my_ns_1 {
char * name = "zhangSan";
int age = 21;
void show() {
std::cout << "my_ns_1 show()" << std::endl;
}
void play() {
std::cout << "my_ns_1 play()" << std::endl;
}
}
namespace my_ns_2 {
void play() {
std::cout << "my_ns_2 play()" << std::endl;
}
}
int main() {
using namespace my_ns_1;
char * my_name1 = my_ns_1::name;
char * my_name2 = name;
show();
my_ns_1::play();
using namespace my_ns_2;
my_ns_2::play();
return 0;
}
const 常量
#include <iostream>
int main() {
const int number = 200;
std::cout << number << std::endl;
return 0;
}
常量指针、指针常量、常量指针常量
const int * numberP1 = &number1;
numberP1 = &number2;
int* const numberP2 = &number1;
*numberP2 = 100;
const int * const numberP3 = &number1;
引用
#include <iostream>
int main() {
int number1 = 10;
int & number2 = number1;
int & number3 = number1;
number2 = 200;
number3 = 300;
std::cout << "地址:" << "number1:" << &number1 << ", number2:" << &number2 << ", number3:" << &number3 << std::endl;
std::cout << "值:" << "number1:" << number1 << ", number2:" << number2 << ", number3:" << number3 << std::endl;
return 0;
}
常量引用
常量引用,无法修改,只读
#include <iostream>
#include <string.h>
typedef struct {
char name[20];
} Student;
void insert(const Student & student) {
std::cout << student.name << std::endl;
}
int main() {
Student student = {"liSi"};
insert(student);
return 0;
}
函数特殊写法
#include <iostream>
void MyTest(char * text, int, double ){
std::cout << text << std::endl;
}
int main() {
MyTest("hello", 1, 2.0);
return 0;
}
可变参数
#include <iostream>
#include <stdarg.h>
void sum(int count, ...){
va_list vp;
va_start(vp, count);
int number1 = va_arg(vp, int);
std::cout << number1 << std::endl;
int number2 = va_arg(vp, int);
std::cout << number2 << std::endl;
va_end(vp);
}
int main() {
sum(999, 10, 21, 32, 41, 5, 62);
return 0;
}
for 遍历
#include <iostream>
#include <stdarg.h>
void sum(int count, ...){
va_list vp;
va_start(vp, count);
for (int i = 0; i < count; ++i) {
std::cout << va_arg(vp, int) << std::endl;
}
va_end(vp);
}
int main() {
sum(6, 10, 21, 32, 41, 5, 62);
return 0;
}
static
#include <iostream>
class People {
public:
char * name;
static int age;
void update1() {
age += 1;
}
static void update2() {
age += 1;
}
};
int People::age = 9;
int main() {
People people;
std::cout << People::age << std::endl;
People::update2();
people.update2();
people.update1();
std::cout << People::age << std::endl;
return 0;
}
this
#include <iostream>
class People {
private:
char * name;
public:
static int id;
public:
void setName(char * name){
this->name = name;
}
char * getName(){
return this->name;
}
};
int People::id = 1001;
int main() {
People people1;
people1.setName("zhangSan1");
people1.id = 2002;
People people2;
people2.setName("zhangSan2");
people2.id = 3003;
std::cout << "people1.getName(): " << people1.getName() << ", id: " << people1.id << std::endl;
std::cout << "people1.getName(): " << people2.getName() << ", id: " << people2.id << std::endl;
std::cout << "People::id: " << People::id << std::endl;
return 0;
}
数组
初始化
#include <iostream>
int main() {
int intArray1[3];
intArray1[0] = 10;
intArray1[1] = 11;
intArray1[2] = 12;
int intArray2[4] = {20, 21, 22, 23};
int intArray3[10] = {1, 15};
int intArray4[5] = {0};
int intArray7[10] = {};
int intArray5[] = {11, 15, 21, 22, 26, 76};
int intArray6[3] {31, 35, 66};
return 0;
}
sizeof
#include <iostream>
int main() {
int intArray1[3];
intArray1[0] = 10;
intArray1[1] = 11;
intArray1[2] = 12;
std::cout << "整个数组的字节大小:" << sizeof(intArray1) << std::endl;
std::cout << "数组中单个元素的字节大小:" << sizeof(intArray1[0]) << std::endl;
int num_elements = sizeof(intArray1) / sizeof(int);
std::cout << "元素个数:" << num_elements << std::endl;
return 0;
}
字符串
初始化
#include <iostream>
int main() {
char str1[5] = {'h', 'e', 'l', 'l', 'o'};
char str2[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
char str3[6] = "hello";
char str4[] = "hello";
return 0;
}
字符串长度
#include <iostream>
#include <cstring>
int main() {
char str1[6] = "ho";
char str2[] = "hello";
std::cout << "str1 长度为:" << strlen(str1) << std::endl;
std::cout << "str2 长度为:" << strlen(str2) << std::endl;
return 0;
}
string类
初始化
#include <iostream>
#include <string>
int main() {
std::string str1 = "hello1";
std::string str2 = {"hello2"};
std::string str3 {"hello3"};
std::string str4("hello4");
std::string str5(10, 'h');
std::string str6(str5);
std::string str7(&str1[3], &str1[5]);
std::string str8(str1, 3, 5);
std::string str9 = str1 + str2;
return 0;
}
字符串长度
#include <iostream>
#include <string>
int main() {
std::string str1 = "hello1";
std::cout << "长度:" << str1.size() << std::endl;
std::cout << "长度:" << str1.length() << std::endl;
return 0;
}
查找
如果查到,返回对应的下标,否则返回-1
#include <iostream>
#include <string>
int main() {
std::string str1 = "hello world c++ java c python";
int index1 = str1.rfind("l");
int index2 = str1.find("l");
int index3 = str1.find("l", 4);
int index4 = str1.rfind("kotlin");
if (index4 == std::string::npos){
std::cout << "没查找到!" << std::endl;
}
return 0;
}
struct 结构
结构初始化
#include <iostream>
#include <string>
struct people {
std::string name;
int age;
};
int main() {
people p1 = {
"zhangSan",
23
};
people p2 = {"liSi",24};
people p3 {"wangWu", 25};
people p4 {};
std::cout << p1.name << ", " << p1.age << std::endl;
std::cout << p2.name << ", " << p2.age << std::endl;
std::cout << p3.name << ", " << p3.age << std::endl;
std::cout << p4.name << ", " << p4.age << std::endl;
return 0;
}
结构数组
#include <iostream>
#include <string>
struct people {
std::string name;
int age;
};
int main() {
people p[2] = {
{"zhangSan", 23},
{"liSi", 24}
};
std::cout << p[0].name << ", " << p[0].age << std::endl;
std::cout << p[1].name << ", " << p[1].age << std::endl;
return 0;
}
enum 枚举
#include <iostream>
enum CommentType {
TEXT = 10,
TEXT_IMAGE,
IMAGE
};
int main() {
enum CommentType type1 = TEXT;
enum CommentType type2 = TEXT_IMAGE;
enum CommentType type3 = IMAGE;
std::cout << type1 << std::endl;
std::cout << type2 << std::endl;
std::cout << type3 << std::endl;
return 0;
}
|