c++复杂数据类型——数组,字符串

数组

    long productIds[199];
    productIds[0] = 20;
    productIds[1] = 30;
    productIds[2] = productIds[0] * productIds[1];
    cout << "数组占的字节数:" << sizeof productIds << endl;
    cout << "数组长度:" << sizeof productIds / sizeof(long) << endl;

	// 初始化
	// 传统初始化
	int number[4] = {3,6,9,12};
    int productIds[4];
    // 传统数组清零
    //  没有进行初始化的数组元素都会设为0
    float values[5] = {5.0,2.1};
    float totals[200] = {0};
    
    //  C++ 11中的清零方式
    float totals1[200] = {};
    //  C++ 11中的数组初始化方式
    double c11_values1[4] {1.2,3.3,4.4,5.5};
    double c11_values2[] {1.2,3.3,4.4,5.5};
    //  c++ 11中的数组元素类型检测
    //short years[] = {25, 12,1.0};
    int x = 123;
    char c_array[] = {'a','b',123};
    char c_array1[] = {'a','b',(char)x};

字符串

1. C风格字符串定义及初始化
	char str1[6] = {'a', 'b', 'c', '\0', 'e', '\0'}; 
	cout << str1 << endl; // abc
	cout << *(str1+4)<<endl; // e
	
	char str2[6] = {'a', 'b', 'c', 'd', 'e'}; // 不是字符串,只是普通字符数组

	char str3[6] = "abcdef"; // 字符长度为6, 末尾\0没有位置,报错
	char str4[6] = "abc"; //字符数必须小于6
	char str5[] = "abcdefghijk"; //不指定字符数,字符长度不限制
	
	char *pstr = "abc";
	cout << pstr << endl; // abc
	
2. C风格字符串字符串连接
	char str6[] = "abc""def";
	cout << str6<< endl; // abcdef
	cout << "abc""def"<< endl; // abcdef

	char str6[] = "abc"pstr;  // 报错,不允许
3.C风格字符串 获取字符串长度
	cout << strlen(str4)<< endl; //输出实际字符长度,并不是定义长度
4. C风格字符串 获取字符串中所有字符
	int n = strlen(str4);
	for (int i = 0; i < n; i++) {
		cout << str3[i] << endl;
		cout << *(str3 + i) << endl;
	}
5. C风格字符串的输入_cin
	int size = 10;
	char name [size]; //存储9个字符+ \0
	cout << "请输入你的名字";
	cin >> name;
	cout << "你的名字:" << name << endl ;
	// 输入:abcdefjabcdefjabcdefg
	// 输出: abcdefjabcdefjabcdefg + 乱码
	// 因为没有\0会一直找,乱码为系统内存找到的,并非我们分配的字符
	name[9] = '0';
	cout << "你的名字:" << name << endl ;
	// 输入:abcdefjabcdefjabcdefg
	// 输出: abcdefjab
	
	// 输入:tom jerry
	// 输出: tom

	char s[20];
	cin >> name >> s;
	cout << "你的名字:" << name << s << endl ;
	// 输入:tom jerry
	// 输出: tomjerry
7. C风格字符串的输入_isstream.getline
	cin.getline(name, 20);
	cout << "你的名字:" << name << endl;
8. C风格字符串的输入_isstream.getline
	char city1[20];
	char city2[20];
	cout << "城市1:";
	cin.get(city1, 20);
	cout << "城市2:";
	cin.get(city2, 20);
	cout << "城市1:" << city1 << endl;
	cout << "城市2:" << city2 << endl;

在这里插入图片描述
get会读取回车,相当于城市2 没有输入,对应的是回车,且并不会显示,解决方案如下:

	cout << "城市1:";
	cin.get(city1, 20);
	cin.get(); 
	//或者 
	cin.get(city1, 20).get();

使用getline不会出现该问题

8. 字符串的输入_与数字混合输入
	int year;
	char name[20];
	cin >> year; // 按数字读取,回车
	cin.getline(name, 20); //把回车当取字符,name输入空行
	cout << "year:" << year << endl;
	cout << "name:" << name << endl;

在这里插入图片描述
解决方案

	(cin >> year).get();
	cin.getline(name, 20);

在这里插入图片描述

9. C++风格字符串声明,初始化,输入输出
	// 声明
	char str1[] = "hello world"; // c
	string str2 = "hello world"; // c++

	// 初始化
	string str3 = {"hello world"};
	string str4 {"hello world"}; // c++ 11

	// 输入输出string变量值
	cout << "请输入str1:";
    cin >> str1;
    cout << "str1=" << str1 << endl;
    cout << "请输入str2:";
    cin >> str2;
    cout << "str2=" << str2 << endl;
	
	// 获取string类型变量的指定字符
	for(int i = 0; i < str2.length();i++)
    {
        cout << "<" << str2[i] << ">" << endl;
    }
10. 字符串复制,连接,获取长度
	//  C++字符串的复制
    string str1 = "abc";
    string str2 = str1;
    cout << "str2 = " << str2 << endl;
    const char *p = str1.c_str();
    cout << "p=" << p << endl;
    //  C字符串的复制
    char c_str1[] = "hello";
    char c_str2[4];
    
    //strcpy(c_str2,c_str1); //长度相同或者大于被复制者
    strncpy(c_str2, c_str1, 3);
    cout << "c_str2=" << c_str2 << endl;
    
    //  C++字符串连接
    str2 += str1; // str2 = str2 + str1;
    cout << "str2=" << str2 << endl;
    
    // C字符串连接
    char c_str3[10] {"hello "};
    char c_str4[] {"world"};
	// strcat(c_str3, c_str4);
    strncat(c_str3, c_str4, 3);
    cout << "c_str3=" << c_str3 << endl;
    
    //  获取字符串的长度
    int len1 = str1.length(); // 长度不包括\0
    int len2 = strlen(c_str3); // 遇到\0结束,返回\0前实际字符的个数
11. 宽字符串
    string str1 = "中花";
    char c_str[] = "中";
    wchar_t wc_str[] = L"中";
    
    cout << "str1 length:" << str1.length() << endl;
    cout << "c_str length:" << sizeof(c_str) / sizeof(char) << endl; // 4个字节包括 中3个, \0 1个
    cout << "c_str byte length:" << sizeof(c_str) << endl; 
    cout << "wc_str length:" << sizeof(wc_str) / sizeof(wchar_t) << endl; 
    cout << "wc_str byte length:" << sizeof(wc_str)  << endl; // 中占用4个字节,\0占用4个字节, 不管什么字符都占用4个字节

    char16_t w16_char[] =u"中";
    cout << "w16_char length:" << sizeof(w16_char) / sizeof(char16_t) << endl;  // 2
    cout << "w16_char byte length:" << sizeof(w16_char)  << endl; // 4 ,中占用2个字节,\0占用2个字节

	//  char32_t = wchar_t

utf8编码格式
gb2312或者gbk编码

12. Raw String
    //  Raw String
    cout << "Hello \n World" << endl;
    // 不想换行,加转义符
    // cout << "Hello \\n World" << endl;
    // 如果多个\n,每个都需要加转义字符很麻烦,使用如下字符会直接输出
    cout << R"(Hello \n \n \ World)" << endl;
    cout << R"xyz((Hello \n (aa)\n \ World))xyz" << endl;

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值