第3章 字符串向量和数组

Posted COCO_PEAK_NOODLE

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第3章 字符串向量和数组相关的知识,希望对你有一定的参考价值。

1-getline()使用

using std::getline;

int main()
{
    for (string str; getline(cin, str); cout << str << endl);
    return 0;
}

2-理解for循环开头的执行顺序 1->2->3

int main()
{
    for (string str/*1*/; cin >> str/*2*/; cout << str << endl/*3*/);
    return 0;
}

3-cin的运算符<<的返回类型是其自身

int main()
{
    string str1, str2;
    while (cin >> str1 >> str2)
    {
        if (str1 == str2)
            cout << "The two strings are equal." << endl;
        else
            cout << "The larger string is " << ((str1 > str2) ? str1 : str2);
    }

    return 0;
}

4-decltype使用
C++11新标准学习:decltype关键字

int main()
{
    string str("a simple string");
    
    // while
    decltype(str.size()) i = 0;
    while (i < str.size()) str[i++] = 'X';
    cout << str << endl;

    // for
    for (i = 0; i < str.size(); str[i++] = 'Y');
    cout << str << endl;

    // I like range for.
    return 0;
}

5-ispunct函数,检测是否是标点符号
ispunct()函数用法及其详解

6-vector vector 初始化 、/b的含义
C语言中的转义字符\\b的含义

int main()
{
    vector<int> v1;
    cout << "{\\n \\"v1\\":{\\"size\\":\\"" << v1.size() << "\\",\\"value\\":[";
    for (auto i : v1)
        cout << i << ",";
    if (!v1.empty()) cout << "\\b";
    cout << "]}" << endl;

    vector<int> v2(10);
    cout << " \\"v2\\":{\\"size\\":\\"" << v2.size() << "\\",\\"value\\":[";
    for (auto i : v2)
        cout << i << ",";
    if (!v2.empty()) cout << "\\b";
    cout << "]}" << endl;

    vector<int> v3(10, 42);
    cout << " \\"v3\\":{\\"size\\":\\"" << v3.size() << "\\",\\"value\\":[";
    for (auto i : v3)
        cout << i << ",";
    if (!v3.empty()) cout << "\\b";
    cout << "]}" << endl;

    vector<int> v4{ 10 };
    cout << " \\"v4\\":{\\"size\\":\\"" << v4.size() << "\\",\\"value\\":[";
    for (auto i : v4)
        cout << i << ",";
    if (!v4.empty()) cout << "\\b";
    cout << "]}" << endl;

    vector<int> v5{ 10, 42 };
    cout << " \\"v5\\":{\\"size\\":\\"" << v5.size() << "\\",\\"value\\":[";
    for (auto i : v5)
        cout << i << ",";
    if (!v5.empty()) cout << "\\b";
    cout << "]}" << endl;

    vector<string> v6{10};
    cout << " \\"v6\\":{\\"size\\":\\"" << v6.size() << "\\",\\"value\\":[";
    for (auto i : v6)
        if (i.empty()) cout << "(null)" << ",";
        else cout << i << ",";
        if (!v6.empty()) cout << "\\b";
        cout << "]}" << endl;

        vector<string> v7{10,"hello"};
        cout << " \\"v7\\":{\\"size\\":\\"" << v7.size() << "\\",\\"value\\":[";
        for (auto i : v7)
            if (i.empty()) cout << "(null)" << ",";
            else cout << i << ",";
            if (!v7.empty()) cout << "\\b";
            cout << "]}\\n}" << endl;

            return 0;
}

输出

{
 "v1":{"size":"0","value":[]}
 "v2":{"size":"10","value":[0,0,0,0,0,0,0,0,0,0]}
 "v3":{"size":"10","value":[42,42,42,42,42,42,42,42,42,42]}
 "v4":{"size":"1","value":[10]}
 "v5":{"size":"2","value":[10,42]}
 "v6":{"size":"10","value":[(null),(null),(null),(null),(null),(null),(null),(null),(null),(null)]}
 "v7":{"size":"10","value":[hello,hello,hello,hello,hello,hello,hello,hello,hello,hello]}
}

7- toupper
C 库函数 - toupper()

8- 代码怎么写优雅

#include <iostream>
#include <vector>

using std::vector; using std::cout; using std::endl; using std::cin;

int main()
{
    vector<int> ivec;
    for (int i; cin >> i; ivec.push_back(i));

    if (ivec.empty())
    {
        cout << "input at least one integer." << endl;
        return -1;
    }

    if (ivec.size() == 1)
    {
        cout << "only one integer " << ivec[0] << ", it doesn't have any adjacent elements." << endl;
        return -1;
    }

    for (int i = 0; i < ivec.size() - 1; ++i)
        cout << ivec[i] + ivec[i + 1] << " ";
    cout << endl;
    
    return 0;
}

8-迭代体返回的是指针

using std::vector; using std::iterator; using std::cout;

int main()
{
    vector<int> v{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    for (auto it = v.begin(); it != v.end(); ++it) *it *= 2;
    for (auto i : v) cout << i << " ";

    return 0;
}

9- 指针指向的对象可以改变其数值

int main()
{
    vector<unsigned> scores(11, 0);
    
    for (unsigned grade = 0; grade < 100; grade += 10)
    {
        std::cout << "grade " << grade << std::endl;
        if (grade <= 100)
            ++*(scores.begin() + grade / 10);
    }

    for (auto s : scores) 
        cout << s << " ";
    cout << endl;

    return 0;
}

输出

grade 0
grade 10
grade 20
grade 30
grade 40
grade 50
grade 60
grade 70
grade 80
grade 90
1 1 1 1 1 1 1 1 1 1 0 

10-指针可以自加自减的操作, int* const pb1表示不可以对指针操作,但是可以给别人赋值,然后再操作。

bool compare(int* const pb1, int* const pe1, int* const pb2, int* const pe2)
{
    if ((pe1 - pb1) != (pe2 - pb2)) // have different size.
        return false;
    else
    {
        for (int* i = pb1, *j = pb2; (i != pe1) && (j != pe2); ++i, ++j)
            if (*i != *j) return false;
    }

    return true;
}

11-比较字符串char* 和 string

int main()
{
    // use string.
    string s1("Mooophy"), s2("Pezy");
    if (s1 == s2)
        cout << "same string." << endl;
    else if (s1 > s2)
        cout << "Mooophy > Pezy" << endl;
    else
        cout << "Mooophy < Pezy" << endl;

    cout << "=========" << endl;

    // use C-Style character strings.
    const char* cs1 = "Wangyue";
    const char* cs2 = "Pezy";
    auto result = strcmp(cs1, cs2);
    if (result == 0)
        cout << "same string." << endl;
    else if (result < 0)
        cout << "Wangyue < Pezy" << endl;
    else
        cout << "Wangyue > Pezy" << endl;

    return 0;
}

12-字符串长度和拼接

const char cstr1[]="Hello";
const char cstr2[]="world!";

int main()
{
    constexpr size_t new_size = strlen(cstr1) + strlen(" ") + strlen(cstr2) +1;
    char cstr3[new_size];
    
    strcpy(cstr3, cstr1);
    strcat(cstr3, " ");
    strcat(cstr3, cstr2);
    
    std::cout << "num " << new_size << std::endl;
    std::cout << cstr3 << std::endl;
}

13 -auto 产生的原因,多重数组打印
原始

int main()
{
    int arr[3][4] = 
    { 
        { 0, 1, 2, 3 },
        { 4, 5, 6, 7 },
        { 8, 9, 10, 11 }
    };

    // range for
    for (const int (&row) [4] : arr)
        for (int col : row) cout << col << " ";
    cout << endl;

    // for loop
    for (size_t i = 0; i != 3; ++i)
        for (size_t j = 0; j != 4; ++j) cout << arr[i][j] << " ";
    cout << endl;

    // using pointers.
    for (int(*row)[4] = arr; row != arr + 3; ++row)
        for (int *col = *row; col != *row + 4; ++col) cout << *col << " ";
    cout << endl;

    return 0;
}

使用using

int main()
{
    int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

    // a range for to manage the iteration
    // use type alias
    using int_array = int[4];
    for (int_array& p : ia)
        for (int q : p)
            cout << q << " ";
    cout << endl;

    // ordinary for loop using subscripts
    for (size_t i = 0; i != 3; ++i)
        for (size_t j = 0; j != 4; ++j)
            cout << ia[i][j] << " ";
    cout << endl;

    // using pointers.
    // use type alias
    for (int_array* p = ia; p != ia + 3; ++p)
        for (int *q = *p; q != *p + 4; ++q)
            cout << *q << " ";
    cout << endl;

    return 0;
}

使用auto

int main()
{
    int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 字符串, 向量和数组 术语表

字符串向量和数组C++

复现经典:《统计学习方法》第 7 章 支持向量机

3D数学基础:图形与游戏开发笔记 第7~8章 矩阵

《R语言实战》第2章

R语言编程艺术R中的数据结构