美文网首页
数组和字符串

数组和字符串

作者: 85dc0dea73fd | 来源:发表于2019-02-20 11:03 被阅读0次

    title: c++之管理数组和字符串
    tags:


    数组和字符串


    什么是数组

    int number [5] = {0};
    char number [5];
    int number [5] = {100,12,15,48,6}
    int number [5] = {};
    int number [5] = {66,55};

    int number [3] [3] = {{515,585,84},{},{45,77,24}} 三行三列

    const int ARRAY_LENGTH = 5;
    int number [ARRAY_LENGTH] = {45,458,48,45,8};
    以上即为数组的声明,上面全部是静态数组,即在编译阶段就已经确定了长度。
    在执行阶段确定长度的数组为动态数组。

    数组的预留空间大小=数组的个数x数组的类型的大小

    访问数组中的元素 N [n] 访问的是n+1个元素,超过的补随机数。

    务必初始化数组,否则将包含未知数,保证在边界内索引。

    使用std::vector初始化动态数组 std::vector<int>dynArray (3);

    C++字符串 使用std::string 初始化字符串、存储用户输入、复制和拼接字符串以及确定字符串长度

    应例

    0: #include <iostream>
    1: #include <string>
    2:
    3: using namespace std;
    5: int main()
    6: {
    7: string greetString ("Hello std::string!");
    8: cout << greetString << endl;
    9:
    10: cout << "Enter a line of text: " << endl;
    11: string firstLine;
    12: getline(cin, firstLine);
    13:
    14: cout << "Enter another: " << endl;
    15: string secondLine;
    16: getline(cin, secondLine);
    17:
    18: cout << "Result of concatenation: " << endl;
    19: string concatString = firstLine + " " + secondLine;
    20: cout << concatString << endl;
    21:
    22: cout << "Copy of concatenated string: " << endl;
    23: string aCopy;
    24: aCopy = concatString;
    25: cout << aCopy << endl;
    26:
    27:
    cout << "Length of concat string: " << concatString.length() << endl;
    28:
    29:
    return 0;
    30: }
    

    image.png

    文章依据21天学通C++第八版,纯属小白自学!!!!

    相关文章

      网友评论

          本文标题:数组和字符串

          本文链接:https://www.haomeiwen.com/subject/fyfqyqtx.html