美文网首页
(第三章)顺序容器(Sequential Containers)

(第三章)顺序容器(Sequential Containers)

作者: Dample_MAN | 来源:发表于2017-03-12 15:05 被阅读0次

    string 表示可变长字符串,它的初始化方式分为两种:直接初始化和拷贝初始化。如果要处理string中的单个字符,可以使用范围for,也可以使用下标运算符执行随机访问

    vectorb 表示同种对象的集合。如果向vector 对象中添加元素,必须使用push_back()函数,不可以使用下标形式添加元素。

    iterator 提供与指针功能类似的间接访问操作,iterator可以进行解引用,与整数相加,比较,两个整数相加减等操作,但是不能直接两个iterator相加。

    Array 表示同类对象的集合。但与vector相比,Array的容量是固定的。

    字符串string用campare()的比较:

    .compare() returns an integer, which is a measure of the difference between the two strings.

    A return value of 0 indicates that the two strings compare as equal.
    A positive value means that the compared string is longer, or the first non-matching character is greater.
    A negative value means that the compared string is shorter, or the first non-matching character is lower.

    字符串单纯的用>,<,==比较的话:

    字符串的比较是逐个相应字符进行比较(比较他们的ASCII码),直到有两个字符不相等为止,ASCII码大的字母所在字符串就大,与字符串长度无关。对两个相等长度的字符串,若每个字符都比较完毕后仍相等,则两字符串相等;对不等长的字符串,若当短的字符串比较完毕时所有字符仍相等,则长度较长的字符串大!

    c++ primer Practise 3.16

    为什么以下程序会忽略getline()的循环???

    #include<vector>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        string s1;
        char cont = 'y';
        vector<string> vString;
        cout << "请输入一个字符串" << endl;
    
        while (cin >> s1)
        {
            cout << s1;
            vString.push_back(s1);
            cout << "是否继续输入(y or n)" << endl;
            cin >> cont ;
            //cin.ignore(1000, '\n');
            if (cont != 'y' && cont != 'Y')
                break;
        }
    
        for (auto c : vString)
            cout << c << " ";
        cout << endl;
    
        return 0;
    }```
    
    **解:**
    >The reference page for ignore() describes the parameters, but perhaps in too technical a way to begin with.
    Quote:
    cplusplus_com wrote:
    Extracts characters from the input sequence and discards them, until either n characters have been extracted, or one compares equal to delim.
    
    
    >enter a number, the user might enter something like "123" and then press enter. All of that, including the newline generated when the enter key is pressed, gets stored in the buffer, "123\n".
    After processing the cin >> nNum, the numeric characters which can be interpreted as an integer are extracted from the buffer, but the newline '\n' isn't part of the number, so it remains behind in the buffer.
    
    >cin.ignore() with no parameters will just read and discard a single character from the buffer, which might be sufficient.
    
    >But what if, when prompted for a number, the user typed 
    " 123 hello \n"? Any leading spaces will be read and discarded, but any trailing characters whether spaces or anything else, will get left behind.
    
    >So we can tell the ignore() function to ignore more than one character. But how many? Well, we don't know what was typed by the user, so I just chose a large number, 1000. cin.ignore(1000) tells the function to read and ignore the next 1000 characters. But hold on, the user may not have typed that many, and the command will sit there waiting for the next 999 characters, that's no good. So we add a delimiter, which tells ignore to stop processing after it has read that particular character. Hence the form I chose, cin.ignore(1000, '\n'); which means ignore up to 1000 characters, or until a newline is found. The number 1000 is entirely arbitrary, I just chose that because it is usually sufficient. 
    
    **产生随机数**
    ```srand()``` 表示的是 Initialize random number generator;
    ```rand()``` 表示的是  Generate random number ;
    ```#include<iostream>
    #include<ctime>
    #include<cstdlib>
    using namespace std;
    
    int main()
    {
        int i;
        srand(time(NULL));
        i = rand() % 10; //取值范围为 0-9
        cout << i << endl;
        return 0;
    }```
    

    相关文章

      网友评论

          本文标题:(第三章)顺序容器(Sequential Containers)

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