分享方式:


<string> 函式

getline
stod
stof
stoi
stol
stold
stoll
stoul
stoull
swap
to_string
to_wstring

getline

從輸入資料流一行一行地擷取字串。

// (1) delimiter as parameter
template <class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
    basic_istream<CharType, Traits>& in_stream,
    basic_string<CharType, Traits, Allocator>& str,
    CharType delimiter);

template <class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
    basic_istream<CharType, Traits>&& in_stream,
    basic_string<CharType, Traits, Allocator>& str,
    const CharType delimiter);

// (2) default delimiter used
template <class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
    basic_istream<CharType, Traits>& in_stream,
    basic_string<CharType, Traits, Allocator>& str);

template <class Allocator, class Traits, class Allocator>
basic_istream<Allocator, Traits>& getline(
    basic_istream<Allocator, Traits>&& in_stream,
    basic_string<Allocator, Traits, Allocator>& str);

參數

in_stream
要擷取字串的輸入資料流。

str
要從輸入資料流讀取字元的字串。

delimiter
行的分隔符號。

傳回值

輸入資料流 in_stream

備註

標記為 (1) 的一對函式簽章,會從 in_stream 擷取字元,直到找到 delimiter,並將字元儲存在 str 中。

標示 (2) 為 newline 的函式簽章組會使用換行符作為預設行分隔符,並做為 getline(in_stream, str, in_stream. widen('\n'))

每對的第二個函式是支持 rvalue 參考的第一個類似函式

當發生下列其中一項時,會停止擷取:

  • 在檔案尾,在此情況下,的內部狀態旗標 in_stream 會設定為 ios_base::eofbit

  • 函式擷取與 相等 delimiter的項目之後。 元素不會放回或附加至受控制序列。

  • 函式擷 str.max_size 取項目之後。 內部狀態旗標 in_stream 會設定為 ios_base::failbit

  • 先前列出的錯誤以外的其他錯誤;內部狀態旗標 in_stream 設定為 ios_base::badbit

如需內部狀態旗標的相關信息,請參閱 ios_base::iostate

如果函式沒有擷取任何元素,in_stream 的內部狀態旗標會設定為 ios_base::failbit。 在任何情況下,getline 會傳回 in_stream

如果擲回例外狀況,in_streamstr 會處於有效狀態。

範例

下列程式碼示範兩種模式的 getline():第一個使用預設的分隔符號 (新行),第二個使用空白做為分隔符號。 檔案結尾字元 (鍵盤上的 Ctrl-Z 鍵) 用來控制 while 迴圈的終止。 這個值會將的內部狀態旗標設定為 eofbit,此旗標cin必須在basic_ios::clear()第二個 ,迴圈才能正常運作。

// compile with: /EHsc /W4
#include <string>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    string str;
    vector<string> v1;
    cout << "Enter a sentence, press ENTER between sentences. (Ctrl-Z to stop): " << endl;
    // Loop until end-of-file (Ctrl-Z) is input, store each sentence in a vector.
    // Default delimiter is the newline character.
    while (getline(cin, str)) {
        v1.push_back(str);
    }

    cout << "The following input was stored with newline delimiter:" << endl;
    for (const auto& p : v1) {
        cout << p << endl;
    }

    cin.clear();

    vector<string> v2;
    // Now try it with a whitespace delimiter
    while (getline(cin, str, ' ')) {
        v2.push_back(str);
    }

    cout << "The following input was stored with whitespace as delimiter:" << endl;
    for (const auto& p : v2) {
        cout << p << endl;
    }
}

stod

將字元序列轉換為 double

double stod(
    const string& str,
    size_t* idx = 0);

double stod(
    const wstring& str,
    size_t* idx = 0
;

參數

str
要轉換的字元序列。

idx
第一個未轉換的字元的索引值。

傳回值

double 值。

備註

函式會呼叫 strtod( str.c_str(), _Eptr),其中 _Eptr 是函式內部的物件,將中的str專案序列轉換成 型double別的值。 如果 str.c_str() == *_Eptr為 ,則會擲回 型 invalid_argument別 的物件。 如果這類呼叫會設定 errno,其會擲回 out_of_range 類型的物件。 否則,如果 idx 不是 Null 指標,函式會 *_Eptr - str.c_str() 儲存並 *idx 傳回 值。

stof

將字元序列轉換為 float。

float stof(
    const string& str,
    size_t* idx = 0);

float stof(
    const wstring& str,
    size_t* idx = 0);

參數

str
要轉換的字元序列。

idx
第一個未轉換的字元的索引值。

傳回值

float 值。

備註

函式會呼叫 strtof( str.c_str(), _Eptr),其中 _Eptr 是函式內部的物件,將中的str專案序列轉換成 型float別的值。 如果 str.c_str() == *_Eptr為 ,則會擲回 型 invalid_argument別 的物件。 如果這類呼叫會設定 errno,其會擲回 out_of_range 類型的物件。 否則,如果 idx 不是 Null 指標,函式會 *_Eptr - str.c_str() 儲存並 *idx 傳回 值。

stoi

將字元序列轉換為整數。

int stoi(
    const string& str,
    size_t* idx = 0,
    int base = 10);

int stoi(
    const wstring& str,
    size_t* idx = 0,
    int base = 10);

傳回值

整數值。

參數

str
要轉換的字元序列。

idx
第一個未轉換的字元的索引值。

base
要使用的數字基底。

備註

函式stoi會將 str 中的字元序列轉換成 型int別的值,並傳回值。 例如,若傳遞字元序列 "10",則 stoi 的傳回值是整數 10。

stoi當以 的方式呼叫 單一位元組字元時,其行為類似於 strtol 單一位元組字元的函式,其中 _Eptr 是函式內部的物件;如果是wcstol寬字元,則以類似的方式呼叫 時為 wcstol(Str.c_str(), _Eptr, idx)strtol( str.c_str(), _Eptr, idx) 如需詳細資訊,請參閱 、 、 wcstol_strtol_l_wcstol_lstrtol

如果 str.c_str() == *_Eptr為 , stoi 則會擲回 型 invalid_argument別 的物件。 如果這類呼叫會設定 errno,或如果傳回的值不能表示為 型 int別的物件,則會擲回 型 out_of_range別的物件。 否則,如果 idx 不是 Null 指標,函式會 *_Eptr - str.c_str() 儲存在中 *idx

stol

將字元序列轉換為 long

long stol(
    const string& str,
    size_t* idx = 0,
    int base = 10);

long stol(
    const wstring& str,
    size_t* idx = 0,
    int base = 10);

參數

str
要轉換的字元序列。

idx
第一個未轉換的字元的索引值。

base
要使用的數字基底。

傳回值

長整數值。

備註

函式會呼叫 strtol( str.c_str(), _Eptr, idx),其中 _Eptr 是函式內部的物件,將 str 中的專案序列轉換成 型long別的值。 如果 str.c_str() == *_Eptr為 ,則會擲回 型 invalid_argument別 的物件。 如果這類呼叫會設定 errno,其會擲回 out_of_range 類型的物件。 否則,如果 idx 不是 Null 指標,函式會 *_Eptr - str.c_str() 儲存並 *idx 傳回 值。

stold

將字元序列轉換為 long double

double stold(
    const string& str,
    size_t* idx = 0);

double stold(
    const wstring& str,
    size_t* idx = 0);

參數

str
要轉換的字元序列。

idx
第一個未轉換的字元的索引值。

傳回值

long double 值。

備註

函式會呼叫 strtold( str.c_str(), _Eptr),其中 _Eptr 是函式內部的物件,將 str 中的專案序列轉換成 型long double別的值。 如果 str.c_str() == *_Eptr為 ,則會擲回 型 invalid_argument別 的物件。 如果這類呼叫會設定 errno,其會擲回 out_of_range 類型的物件。 否則,如果 idx 不是 Null 指標,函式會 *_Eptr - str.c_str() 儲存並 *idx 傳回 值。

stoll

將字元序列轉換為 long long

long long stoll(
    const string& str,
    size_t* idx = 0,
    int base = 10);

long long stoll(
    const wstring& str,
    size_t* idx = 0,
    int base = 10);

參數

str
要轉換的字元序列。

idx
第一個未轉換的字元的索引值。

base
要使用的數字基底。

傳回值

long long 值。

備註

函式會呼叫 strtoll( str.c_str(), _Eptr, idx),其中 _Eptr 是函式內部的物件,將 str 中的專案序列轉換成 型long long別的值。 如果 str.c_str() == *_Eptr為 ,則會擲回 型 invalid_argument別 的物件。 如果這類呼叫會設定 errno,其會擲回 out_of_range 類型的物件。 否則,如果 idx 不是 Null 指標,函式會 *_Eptr - str.c_str() 儲存在中 *idx 並傳回值。

stoul

將字元序列轉換成不帶正負號的 long。

unsigned long stoul(
    const string& str,
    size_t* idx = 0,
    int base = 10);

unsigned long stoul(
    const wstring& str,
    size_t* idx = 0,
    int base = 10);

參數

str
要轉換的字元序列。

idx
第一個未轉換的字元的索引值。

base
要使用的數字基底。

傳回值

不帶正負號的 long 整數值。

備註

函式會呼叫 strtoul( str.c_str(), _Eptr, idx),其中 _Eptr 是函式內部的物件,將 str 中的專案序列轉換成 型unsigned long別的值。 如果 str.c_str() == *_Eptr為 ,則會擲回 型 invalid_argument別 的物件。 如果這類呼叫會設定 errno,其會擲回 out_of_range 類型的物件。 否則,如果 idx 不是 Null 指標,函式會 *_Eptr - str.c_str() 儲存在中 *idx 並傳回值。

stoull

將字元序列轉換為 unsigned long long

unsigned long long stoull(
    const string& str,
    size_t* idx = 0,
    int base = 10);

unsigned long long stoull(
    const wstring& str,
    size_t* idx = 0,
    int base = 10);

參數

str
要轉換的字元序列。

idx
第一個未轉換的字元的索引值。

base
要使用的數字基底。

傳回值

unsigned long long 值。

備註

函式會呼叫 strtoull( str.c_str(), _Eptr, idx),其中 _Eptr 是函式內部的物件,將 str 中的專案序列轉換成 型unsigned long long別的值。 如果 str.c_str() == *_Eptr為 ,則會擲回 型 invalid_argument別 的物件。 如果這類呼叫會設定 errno,其會擲回 out_of_range 類型的物件。 否則,如果 idx 不是 Null 指標,函式會 *_Eptr - str.c_str() 儲存並 *idx 傳回 值。

swap

交換兩個字串的字元陣列。

template <class Traits, class Allocator>
void swap(basic_string<CharType, Traits, Allocator>& left, basic_string<CharType, Traits, Allocator>& right);

參數

left
一個字串,其專案要與另一個字串的元素交換。

right
另一個字串,其元素要與第一個字串交換。

備註

範本函式會針對字串執行特製化成員函 left.swap式 (right),其保證常數複雜度。

範例

// string_swap.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( )
{
   using namespace std;
   // Declaring an object of type basic_string<char>
   string s1 ( "Tweedledee" );
   string s2 ( "Tweedledum" );
   cout << "Before swapping string s1 and s2:" << endl;
   cout << "The basic_string s1 = " << s1 << "." << endl;
   cout << "The basic_string s2 = " << s2 << "." << endl;

   swap ( s1 , s2 );
   cout << "\nAfter swapping string s1 and s2:" << endl;
   cout << "The basic_string s1 = " << s1 << "." << endl;
   cout << "The basic_string s2 = " << s2 << "." << endl;
}
Before swapping string s1 and s2:
The basic_string s1 = Tweedledee.
The basic_string s2 = Tweedledum.

After swapping string s1 and s2:
The basic_string s1 = Tweedledum.
The basic_string s2 = Tweedledee.

to_string

將值轉換成 string

string to_string(int value);
string to_string(unsigned int value);
string to_string(long value);
string to_string(unsigned long value);
string to_string(long long value);
string to_string(unsigned long long value);
string to_string(float value);
string to_string(double value);
string to_string(long double value);

參數

value
要轉換的值。

傳回值

表示值的 string

備註

函式會將 轉換成數位物件 Buf 中儲存在函式內部的專案序列,如同呼叫 sprintf(Buf, Fmt, value),其中 Fmt

  • "%d" 如果 value 類型為 ,則為 int

  • "%u" 如果 value 類型為 ,則為 unsigned int

  • "%ld" 如果 value 類型為 ,則為 long

  • "%lu" 如果 value 類型為 ,則為 unsigned long

  • "%lld" 如果 value 類型為 ,則為 long long

  • "%llu" 如果 value 類型為 ,則為 unsigned long long

  • "%f"如果 為 類型float,則value為 或double

  • "%Lf" 如果 value 類型為 ,則為 long double

函式會傳回 string(Buf)

to_wstring

將值轉換成寬字串。

wstring to_wstring(int value);
wstring to_wstring(unsigned int value);
wstring to_wstring(long value);
wstring to_wstring(unsigned long value);
wstring to_wstring(long long value);
wstring to_wstring(unsigned long long value);
wstring to_wstring(float value);
wstring to_wstring(double value);
wstring to_wstring(long double value);

參數

value
要轉換的值。

傳回值

表示值的寬字串。

備註

函式會將 value 轉換為函式內部的 Buf 陣列物件所儲存的項目序列,就像呼叫 swprintf(Buf, Len, Fmt, value) 一樣,其中 Fmt

  • L"%d" 如果 value 類型為 ,則為 int

  • L"%u" 如果 value 類型為 ,則為 unsigned int

  • L"%ld" 如果 value 類型為 ,則為 long

  • L"%lu" 如果 value 類型為 ,則為 unsigned long

  • L"%lld" 如果 value 類型為 ,則為 long long

  • L"%llu" 如果 value 類型為 ,則為 unsigned long long

  • L"%f"如果 為 類型float,則value為 或double

  • L"%Lf" 如果 value 類型為 ,則為 long double

函式會傳回 wstring(Buf)

另請參閱

<string>