basic_string::append (STL Samples)

Viene illustrato come utilizzare la funzione della libreria di modelli (STL) standard di basic_string:: aggiungere in Visual C++.

string& append(
   const basic_string& _X
);
string& append(
   const basic_string& _X,
   size_type pos,
   size_type count
);
string& append(
   const element_type *_S,
   size_type count
);
string& append(
   const element_type *_S
);
string& append(
   size_type count,
   element_type _C
);
string& append(
   Iterator first,
   Iterator last
);

Note

[!NOTA]

La classe/nomi di parametro nel prototipo non corrisponde alla versione nel file di intestazione.alcuni sono stati modificati per migliorare la leggibilità.

Le funzioni membro di aggiungere degli elementi di basic_string::append alla fine della stringa.I form diverse della funzione forniscono modalità alternative specificare l'origine degli elementi che saranno accodati.Le funzioni di aggiungere restituiscono un riferimento alla stringa in cui elementi sono stati aggiunti.

Esempio

// bsappend.cpp
// compile with: /EHsc
// 
// Functions:
//    string::append - appends a sequence of elements to the
//                           current string.
//////////////////////////////////////////////////////////////////////

#include <string>
#include <iostream>

using namespace std ;

int main()
{
    string str1("012");
    string str2("345");

    cout << "str1 = " << str1.c_str() << endl;

    // append str2 to str1
    str1.append(str2);

    cout << "str1 = " << str1.c_str() << endl;

    // append the last 2 items in str2 to str1
    str2 = "567";
    str1.append(str2, 1, 2);    // begin at pos 1, append 2 elements

    cout << "str1 = " << str1.c_str() << endl;

    // append the first 2 items from an array of the element type
    char achTest[] = {'8', '9', 'A'};
    str1.append(achTest, 2);

    cout << "str1 = " << str1.c_str() << endl;

    // append all of a string literal to str1
    char szTest[] = "ABC";
    str1.append(szTest);

    cout << "str1 = " << str1.c_str() << endl;

    // append one item of the element type
    str1.append(1, 'D');

    cout << "str1 = " << str1.c_str() << endl;

    // append str2 to str1 using iterators
    str2 = "EF";
    str1.append (str2.begin(), str2.end());

    cout << "str1 = " << str1.c_str() << endl;
}

Output

str1 = 012
str1 = 012345
str1 = 01234567
str1 = 0123456789
str1 = 0123456789ABC
str1 = 0123456789ABCD
str1 = 0123456789ABCDEF

Requisiti

intestazione: <stringa>

Vedere anche

Concetti

Esempi di una libreria di modelli standard