string 表示可變長字符串涕蜂,它的初始化方式分為兩種:直接初始化和拷貝初始化析校。如果要處理string中的單個字符,可以使用范圍for长酗,也可以使用下標運算符執(zhí)行隨機訪問溪北。
vectorb 表示同種對象的集合。如果向vector 對象中添加元素花枫,必須使用push_back()函數(shù)刻盐,不可以使用下標形式添加元素。
iterator 提供與指針功能類似的間接訪問操作劳翰,iterator可以進行解引用,與整數(shù)相加馒疹,比較佳簸,兩個整數(shù)相加減等操作,但是不能直接兩個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碼大的字母所在字符串就大衔峰,與字符串長度無關(guān)佩脊。對兩個相等長度的字符串,若每個字符都比較完畢后仍相等垫卤,則兩字符串相等威彰;對不等長的字符串,若當短的字符串比較完畢時所有字符仍相等穴肘,則長度較長的字符串大歇盼!
c++ primer Practise 3.16
為什么以下程序會忽略getline()
的循環(huán)?评抚?豹缀?
#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 << "是否繼續(xù)輸入(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.
**產(chǎn)生隨機數(shù)**
```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;
}```