Espaços nominais
Variantes
Acções

Diferenças entre edições de "cpp/string/basic string/replace"

Da cppreference.com
< cpp‎ | string‎ | basic string
(Import from dokuwiki)
 
m (Use {{lc}}. Update links. Various fixes.)
 
(4 edições intermédias de 3 utilizadores não apresentadas)
Linha 1: Linha 1:
{{title|replace}}
+
{{
Sintaxe:
+
title|replace}}
 +
 +
 +
 +
 +
  
<syntaxhighlight lang="cpp">
+
& replace( , ,
    #include <string>
+
const & str );
    string& replace( size_type index, size_type num, const string& str );
+
    string& replace( size_type index1, size_type num1, const string& str, size_type index2, size_type num2 );
+
num
    string& replace( size_type index, size_type num, const charT* str );
+
& replace( size_type , size_type ,
    string& replace( size_type index, size_type num1, const charT* str, size_type num2 );
+
const str,
    string& replace( size_type index, size_type num1, size_type num2, charT ch);
+
size_type , size_type );
  
    string& replace( iterator start, iterator end, const string& str );
+
    string& replace( iterator start, iterator end, const charT* str );
+
& replace( , ,
    string& replace( iterator start, iterator end, const charT* str, size_type num );
+
, );
    string& replace( iterator start, iterator end, size_type num, charT ch );
+
    string& replace( iterator start, iterator end, input_iterator start2, input_iterator end2 );
+
num
</syntaxhighlight>
+
& replace( , size_type ,
A função replace():
+
, );
  
* substitui até ''num'' caracteres da string actual com caracteres de ''str'', começando em ''index'',
+
, ,
* substitui até ''num1'' caracteres da string actual (começando em ''index1'') com no máximo ''num2'' caracteres de ''str'' começando em ''index2'',
+
* )
* substitui até ''num'' caracteres da string actual com caracteres de ''str'', começando na posição ''index'' em ''str'',
+
* substitui até ''num1'' caracteres da string actual (começando em ''index1'') com ''num2'' caracteres de ''str'' começando em ''index2'',
+
num
* substitui até ''num1'' caracteres na string actual (começando em ''index'') com ''num2'' cópias de ''ch'',
+
( ,
* substitui os caracteres da string actual de ''start'' a ''end'' com ''str'',
+
* )
* substitui caracteres na string actual de ''start'' a ''end'' com ''num'' caracteres de ''str'',
+
* ou substitui os caracteres na string actual de ''start'' a ''end com ''num'' cópias de ''ch''.
+
  
Por exemplo, o código seguinte mostra a string "They say he carved it himself...find your soul-mate, Homer."
+
, ,
 +
 +
 +
 +
 +
  
<syntaxhighlight lang="cpp">
+
    string s = "They say he carved it himself...from a BIGGER spoon";
+
;
    string s2 = "find your soul-mate, Homer.";
+
    s.replace( 32, s.length() - 32, s2 );
+
=
    cout << s << endl;
+
replace( , ,  
</syntaxhighlight>
+
<;
 +
 +
  
Tópicos Relacionados: [[cpp/string/basic_string/insert | insert]]
+
 +
 
 +
:
 +
 
 +
 +
 
 +
[[
 +
 
 +
 +
 
 +
 +
 
 +
 +
 
 +
 +
 
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 
 +
 +
 +
 
 +
 +
 +
 
 +
 +
 
 +
 +
 +
 +
 +
 +
 
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 
 +
cpp/string//
 +
 +
 +
 +
 +
 +
 +
]]

Edição actual desde as 10h23min de 2 de julho de 2013

 
 
Biblioteca cordas
Strings terminadas
Original:
Null-terminated strings
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Cadeias de bytes
Multibyte cordas
Cordas de largura
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
std::basic_string
Funções de membro
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_string::basic_string
basic_string::operator=
basic_string::assign
basic_string::get_allocator
acesso. Elemento
Original:
Element access
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_string::at
basic_string::operator[]
basic_string::front(C++11)
basic_string::back(C++11)
basic_string::data
basic_string::c_str
Iteradores
Original:
Iterators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_string::begin
basic_string::cbegin

(C++11)
basic_string::end
basic_string::cend

(C++11)
basic_string::rbegin
basic_string::crbegin

(C++11)
basic_string::rend
basic_string::crend

(C++11)
Capacidade
Original:
Capacity
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_string::empty
basic_string::size
basic_string::length
basic_string::max_size
basic_string::reserve
basic_string::capacity
basic_string::shrink_to_fit(C++11)
Operações
Original:
Operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_string::clear
basic_string::insert
basic_string::erase
basic_string::push_back
basic_string::pop_back(C++11)
basic_string::append
basic_string::operator+=
basic_string::compare
basic_string::replace
basic_string::substr
basic_string::copy
basic_string::resize
basic_string::swap
Pesquisar
Original:
Search
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_string::find
basic_string::rfind
basic_string::find_first_of
basic_string::find_first_not_of
basic_string::find_last_of
basic_string::find_last_not_of
Constantes
Original:
Constants
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_string::npos
Não-membros funções
Original:
Non-member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Classes auxiliares
Original:
Helper classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
basic_string& replace( size_type pos, size_type count,

                       const basic_string& str );

basic_string& replace( const_iterator first, const_iterator last,

                       const basic_string& str );
(1)
basic_string& replace( size_type pos, size_type count,

                       const basic_string& str,
                       size_type pos2, size_type count2 );

template< class InputIt >
basic_string& replace( const_iterator first, const_iterator last,

                       InputIt first2, InputIt last2 );
(2)
basic_string& replace( size_type pos, size_type count,

                       const CharT* cstr, size_type count2 );

basic_string& replace( const_iterator first, const_iterator last,

                       const CharT* cstr, size_type count2 );
(3)
basic_string& replace( size_type pos, size_type count,

                       const CharT* cstr );

basic_string& replace( const_iterator first, const_iterator last,

                       const CharT* cstr );
(4)
basic_string& replace( size_type pos, size_type count,

                       size_type count2, CharT ch );

basic_string& replace( const_iterator first, const_iterator last,

                       size_type count2, CharT ch );
(5)
basic_string& replace( const_iterator first, const_iterator last,
                       std::initializer_list<CharT> ilist );
(6) (desde C++11)
Substitui a parte da corda indicado por [pos, pos + count) ou [first, last) com uma nova seqüência.
Original:
Replaces the part of the string indicated by either [pos, pos + count) or [first, last) with a new string.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
A nova seqüência pode ser um dos seguintes:
Original:
The new string can be one of:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
str string
Original:
string str
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
substring [pos2, pos2 + count2) de str ou caracteres na [first2, last2) alcance
Original:
substring [pos2, pos2 + count2) of str or characters in the range [first2, last2)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
charcters count2 primeiro da cadeia de caracteres apontada por cstr
Original:
first count2 charcters of the character string pointed to by cstr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
terminada em nulo cadeia de caracteres apontada por cstr
Original:
null-terminated character string pointed to by cstr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
count2 cópias de ch personagem
Original:
count2 copies of character ch
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
6)
caracteres na ilist lista de inicializador
Original:
characters in the initializer list ilist
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Índice

Parâmetros

pos -
início da subsequência que vai ser substituído
Original:
start of the substring that is going to be replaced
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
count -
comprimento da subsequência que vai ser substituído
Original:
length of the substring that is going to be replaced
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
first, last -
intervalo de caracteres que vai ser substituído
Original:
range of characters that is going to be replaced
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
str -
string para substituição
Original:
string to use for replacement
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pos2 -
início da substring para substituir com
Original:
start of the substring to replace with
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
count2 -
número de caracteres para substituir com
Original:
number of characters to replace with
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cstr -
ponteiro para a cadeia de caracteres a ser usado para substituição
Original:
pointer to the character string to use for replacement
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ch -
caráter valor a ser usado para a substituição
Original:
character value to use for replacement
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
first2, last2 -
intervalo de caracteres a serem usados ​​para substituição
Original:
range of characters to use for replacement
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
init -
lista de inicializador com os personagens usam para substituição
Original:
initializer list with the characters to use for replacement
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Type requirements
-
InputIt must meet the requirements of InputIterator.

Valor de retorno

*this

Exceções

std::out_of_range if pos > length() or pos2 > str.length()

std::length_error se a string resultante será superior a seqüência de comprimento máximo possível (std::string::npos - 1)
Original:
std::length_error if the resulting string will exceed maximum possible string length (std::string::npos - 1)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Exemplo

#include <iostream>
#include <string>
 
int main()
{
    std::string str("The quick brown fox jumps over the lazy dog.");
 
    str.replace(10, 5, "red"); // (4)
 
    str.replace(str.begin(), str.begin() + 3, 1, 'A'); // (5)
 
    std::cout << str << '\n';
}

Saída:

A quick red fox jumps over the lazy dog.