zoukankan      html  css  js  c++  java
  • c++第十二天

    《c++ primer, 5E》

    第81页到第86页,笔记:

     处理string对象中的字符:

    1、cctype

    2、foreach(range for)

    3、decltype(Declared Type)

    4、auto

    5、string::size_type(s.size函数的返回值类型)

    6、使用range for改变字符串中的字符,必须把循环变量定义成引用类型

    7、访问string对象的单个字符:下标(也叫索引,类型为string::size_type)和迭代器

        for(decltype(s.size()) index = 0;
            index != s.size() && ! isspace(s[index]); ++index)
                s[index] = toupper(s[index]);

    8、逻辑与运算符(&&)对这个运算符来说最重要的一点是,c++语言规定只有当左侧运算对象为真时才会检查右侧运算对象的情况。

    9、使用下标执行随机访问

        const string hexdigits = "0123456789ABCDEF";
        string result;
        string::size_type n;
        while(cin >> n)
            if(n < hexdigits.size())
                result += hexdigits[n];
        cout << "Your hex number is: " << result << endl;

    遇到的问题:

    1、不需要inlcude<cctype>就可以使用toupper(c)?

    2、下面的程序可编译,无法运行

    #include<string>
    using std::string;
    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    #include <typeinfo>
    int main()
    {
        const string s = "Keep out!";
        for(auto &c: s){
            cout << typeid(c).name() << endl;
        }
        return 0;
    }

    课后练习:

    练习3.6

    #include<string>
    using std::string;
    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main()
    {
        string s("Hello World!!!");
        for(auto &c: s)
            c = 'X';
        cout << s << endl;
        return 0;
    }

    练习3.7

    如果是char c那么不会改变原字符串,

    如果是char &c效果应该和上一题一样。

    (验证正确)

    练习3.8

    遍历序列中的所有元素range for要比while和传统for方便,而且可读性更好。

    练习3.9

    对于这种情况,编译器不会检查下标的合法性,能编译,但是输出结果无法预知。

    练习3.10

    #include<string>
    using std::string;
    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    int main()
    {
        string s;
        getline(cin , s);
        for(auto c: s)
            if(!ispunct(c))
                cout << c;
        cout << endl;
        return 0;
    }

    练习3.11

    合法。应该是底层const

  • 相关阅读:
    Font Awesome 中文网
    mobileselect学习
    JavaScript模块化
    webpack基本使用
    MVVM架构方式
    http-server开启测试服务器
    json-server模拟服务器API
    vue-router
    git的使用
    Vue生命周期
  • 原文地址:https://www.cnblogs.com/xkxf/p/6376736.html
Copyright © 2011-2022 走看看