std::string 输入方法——摘自ChatGPT

You can input characters into a `std::string` variable in C++ using various methods. The most common ways include using `std::cin`, `getline`, or appending characters manually. Here’s how you can do it:

### 1. **Using `std::cin` to Input a Single Word**

If you want to input a single word into a `std::string`, you can simply use `std::cin`.

```cpp
#include <iostream>
#include <string>

int main() {
    std::string str;
    
    std::cout << "Enter a word: ";
    std::cin >> str;
    
    std::cout << "You entered: " << str << std::endl;
    
    return 0;
}
```

**Explanation**:
- `std::cin >> str;` reads a single word (i.e., until a space or newline is encountered) into the `str` variable.

### 2. **Using `std::getline` for Inputting an Entire Line**

If you want to input a full line of text (including spaces), use `std::getline`.

```cpp
#include <iostream>
#include <string>

int main() {
    std::string str;
    
    std::cout << "Enter a line: ";
    std::getline(std::cin, str);
    
    std::cout << "You entered: " << str << std::endl;
    
    return 0;
}
```

**Explanation**:
- `std::getline(std::cin, str);` reads an entire line of input into `str`, stopping when it encounters a newline.

### 3. **Appending Characters to a `std::string`**

You can also build a `std::string` by appending characters one by one.

```cpp
#include <iostream>
#include <string>

int main() {
    std::string str;
    char ch;
    
    std::cout << "Enter characters (type '.' to stop): ";
    while (std::cin >> ch && ch != '.') {
        str += ch;  // Append character to string
    }
    
    std::cout << "You built the string: " << str << std::endl;
    
    return 0;
}
```

**Explanation**:
- `str += ch;` appends the character `ch` to the end of the string `str`.

### 4. **Handling Multiple Words with `std::getline` after `std::cin`**

When using both `std::cin` and `std::getline`, it's important to handle the newline character that `std::cin` leaves in the input buffer. You can do this by calling `std::cin.ignore()` before `std::getline`:

```cpp
#include <iostream>
#include <string>

int main() {
    std::string word;
    std::string line;
    
    std::cout << "Enter a word: ";
    std::cin >> word;
    
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // Ignore leftover newline
    
    std::cout << "Enter a line: ";
    std::getline(std::cin, line);
    
    std::cout << "Word: " << word << std::endl;
    std::cout << "Line: " << line << std::endl;
    
    return 0;
}
```

**Explanation**:
- `std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');` discards the newline character left in the input buffer after `std::cin >> word`.

### 5. **Input Characters One by One into a `std::string`**

If you want to input characters one by one and append them to a `std::string`, you can use a loop:

```cpp
#include <iostream>
#include <string>

int main() {
    std::string str;
    char ch;

    std::cout << "Enter characters (end with a newline): ";
    while (std::cin.get(ch) && ch != '\n') {
        str.push_back(ch);  // Append each character to the string
    }
    
    std::cout << "You entered: " << str << std::endl;

    return 0;
}
```

**Explanation**:
- `std::cin.get(ch)` reads a single character from input, including whitespace.
- `str.push_back(ch);` appends the character to the end of the `str` string.

### Summary

- **Single Word Input**: Use `std::cin >> str;` for single-word input (spaces act as delimiters).
- **Entire Line Input**: Use `std::getline(std::cin, str);` to input an entire line, including spaces.
- **Character-by-Character Input**: You can manually append characters to a `std::string` using `+=` or `push_back()` within a loop.

Each method is useful depending on whether you want to handle spaces, multiple lines, or individual characters in your input.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值