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

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;
}```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,131评论 0 23
  • 不过是吃了几个果冻和瑞士卷,嘴里便全部都是甜食的味道了,又黏又腻的感觉,十分的不舒服。顾不得去刷牙,倒在床上,...
    懿吟迦阅读 264评论 0 0
  • 瓜子 茶余饭后必不可少 闲聊闲扯必不可少 有嚼劲 越吃越香 尤其是无味儿的 抓一把瓜子聊个闲嗑 扯一下午 但...
    画有几画阅读 583评论 2 9
  • 人际关系 我是一很有时间观念的人。今天公司说有个畅销会,我一大早就去了,可惜去了之后老师正在布置会场,一直跟着忙前...
    思晏阅读 437评论 0 0