Compartir a través de


basic_string::assign

Asigna nuevos valores de caracteres al contenido de una cadena.

basic_string<CharType, Traits, Allocator>& assign(
    const value_type* _Ptr
);
basic_string<CharType, Traits, Allocator>& assign(
    const value_type* _Ptr,
    size_type _Count
);
basic_string<CharType, Traits, Allocator>& assign(
    const basic_string<CharType, Traits, Allocator>& _Str,
    size_type off, 
    size_type _Count
);
basic_string<CharType, Traits, Allocator>& assign(
    const basic_string<CharType, Traits, Allocator>& _Str
);
basic_string<CharType, Traits, Allocator>& assign(
    size_type _Count, 
    value_type _Ch
);
template<class InIt>
    basic_string<CharType, Traits, Allocator>& assign(
        InputIterator _First, 
        InputIterator _Last
    );
basic_string<CharType, Traits, Allocator>& assign(
    const_pointer _First,
    const_pointer _Last
);
basic_string<CharType, Traits, Allocator>& assign(
    const_iterator _First,
    const_iterator _Last
);

Parámetros

  • _Ptr
    Un puntero a los caracteres de la C cadena que se va a asignar a la cadena de destino.

  • _Count
    El número de caracteres que se anexará, como máximo, de la cadena de origen.

  • _Str
    La cadena de origen cuyos caracteres deben asignarse a la cadena de destino.

  • _Ch
    El carácter que se va a asignar.

  • _First
    Un iterador, un const_pointer, o un const_iterator de entrada que dirige el primer carácter del intervalo de la cadena de origen que se va a asignar el intervalo de destino.

  • _Last
    Un iterador, un const_pointer, o un const_iterator de entrada que dirige el que es más allá del último carácter del intervalo de la cadena de origen que se va a asignar el intervalo de destino.

  • off
    Posición en la que los caracteres nuevos comenzarán para asignar.

Valor devuelto

Una referencia al objeto de cadena que se está asignado nuevos caracteres por la función miembro.

Comentarios

Las cadenas se pueden asignar nuevos valores de caracteres.El nuevo valor puede ser una cadena y C cadena o un carácter individual.operator= puede usarse si el nuevo valor se puede describir por un único parámetro; si no la función asignaciónde miembro, que tiene varios parámetros, se puede utilizar para especificar qué parte de la cadena debe estar asignada a una cadena de destino.

Ejemplo

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

int main( ) 
{
   using namespace std;

   // The first member function assigning the
   // characters of a C-string to a string
   string str1a;
   const char *cstr1a = "Out There";
   cout << "The C-string cstr1a is: " << cstr1a <<  "." << endl;
   str1a.assign ( cstr1a );
   cout << "Assigning the C-string cstr1a to string str1 gives: " 
        << str1a << "." << endl << endl;

   // The second member function assigning a specific
   // number of the of characters a C-string to a string
   string  str1b;
   const char *cstr1b = "Out There";
   cout << "The C-string cstr1b is: " << cstr1b << endl;
   str1b.assign ( cstr1b , 3 );
   cout << "Assigning the 1st part of the C-string cstr1b "
        << "to string str1 gives: " << str1b << "." 
        << endl << endl;

   // The third member function assigning a specific number
   // of the characters from one string to another string 
   string str1c ( "Hello " ), str2c ( "Wide World " );
   cout << "The string str2c is: " << str2c << endl;
   str1c.assign ( str2c , 5 , 5 );
   cout << "The newly assigned string str1 is: " 
        << str1c << "." << endl << endl;

   // The fourth member function assigning the characters
   // from one string to another string in two equivalent
   // ways, comparing the assign and operator =
   string str1d ( "Hello" ), str2d ( "Wide" ), str3d ( "World" );
   cout << "The original string str1 is: " << str1d << "." << endl;
   cout << "The string str2d is: " << str2d << endl;
   str1d.assign ( str2d );
   cout << "The string str1 newly assigned with string str2d is: " 
        << str1d << "." << endl;
   cout << "The string str3d is: " << str3d << "." << endl;
   str1d = str3d;
   cout << "The string str1 reassigned with string str3d is: " 
        << str1d << "." << endl << endl;

   // The fifth member function assigning a specific 
   // number of characters of a certain value to a string
   string str1e ( "Hello " );
   str1e.assign ( 4 , '!' );
   cout << "The string str1 assigned with eclamations is: " 
        << str1e << endl << endl;

   // The sixth member function assigning the value from
   // the range of one string to another string
   string str1f ( "Hello " ), str2f ( "Wide World " );
   cout << "The string str2f is: " << str2f << endl;
   str1f.assign ( str2f.begin ( ) + 5 , str2f.end ( ) - 1 );
   cout << "The string str1 assigned a range of string str2f is: " 
        << str1f << "." << endl << endl;
}
  
  
  
  
  
  
  
  
  
  

Requisitos

encabezado: <cadena>

espacio de nombres: std

Vea también

Referencia

basic_string Class