Namespace
Varianti

cpp/io/basic stringstream/str: differenze tra le versioni

Da cppreference.com.
m (una revisione importata: Import from Dokuwiki)
m (Use {{lc}}. Update links. Various fixes.)
 
(4 revisioni intermedie di 3 utenti non mostrate)
Riga 1: Riga 1:
{{title|str}}
+
{{|str}}
Sintassi:
+
  
<syntaxhighlight lang="cpp">
+
cpp/str
    #include <sstream>
+
:cpp/str
    void str( const string& s );
+
:cpp/stringstreamstr
    string str();
+
:cpp//str
</syntaxhighlight>
+
:///str
 
+
:///
La funzione str() può essere usata in due modi: il primo per avere una copia della stringa che viene manipolata
+
[[cpp/io//]]
all'interno dello stream:
+
[[cpp/io/]]
 
+
<syntaxhighlight lang="cpp">
+
    ostringstream stream1;
+
    stream1 << "Testing!" << endl;
+
    cout << stream1.str();
+
</syntaxhighlight>
+
 
+
Nel secondo str() può essere usata per copiare una stringa dentro lo stream. Questo metodo è utile soprattutto
+
con gli input streams. Esempio:
+
 
+
<syntaxhighlight lang="cpp">
+
    istringstream stream1;
+
    string string1 = "25";
+
    stream1.str(string1);
+
</syntaxhighlight>
+
 
+
N.B.: Questa funzione lascia i puntatori "get" e "put" impostati a 0.
+
Questo talvolta conduce a risultati confondenti quando si inizializza una
+
stringstream per operazioni bidirezionali.
+
 
+
str() è utile anche quando è necessario azzerare lo strem cosicchè possa essere riutilizzato.
+
__Attenzione__: la funzione membro ''clear()'' **non** cancella i
+
contenuti dell'oggetto stringa contenuto nello stream, semplicemente
+
si limita ad effettuare il clear dello stato dell'oggetto ios, cioè esegue la funzione equivalente di
+
basic_ios::clear().
+
 
+
<syntaxhighlight lang="cpp">
+
    istringstream stream1;
+
    float num;
+
 
+
    // use it once
+
    string string1 = "25 1 3.235\n1111111\n222222";
+
    stream1.str(string1);
+
    while( stream1 >> num ) cout << "num: " << num << endl;  // displays numbers, one per line
+
 
+
    // use the same string stream again with str()
+
    string string2 = "1 2 3 4 5  6 7 8 9 10";
+
    stream1.clear(); // not what you think!  But still required to clear the stream's IO state
+
    stream1.str(string2);
+
 
+
    while( stream1 >> num ) cout << "num: " << num << endl;  // displays numbers, one per line
+
</syntaxhighlight>
+
 
+
Argomenti collegati: [[cpp/io/basic_stringstream/rdbuf | rdbuf]], [[cpp/io | C++ I/O Streams]]
+

Versione attuale delle 13:32, 2 lug 2013

 
 
Ingresso / libreria di output
I / O manipolatori
C-style I / O
Buffer
Original:
Buffers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_streambuf
basic_filebuf
basic_stringbuf
strstreambuf(deprecato)
Streams
Original:
Streams
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Astrazioni
Original:
Abstractions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ios_base
basic_ios
basic_istream
basic_ostream
basic_iostream
File I / O
Original:
File I/O
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_ifstream
basic_ofstream
basic_fstream
String I / O
Original:
String I/O
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_istringstream
basic_ostringstream
basic_stringstream
Array I / O
Original:
Array I/O
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
istrstream(deprecato)
ostrstream(deprecato)
strstream(deprecato)
Tipi
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
streamoff
streamsize
fpos
Errore categoria interfaccia
Original:
Error category interface
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
iostream_category(C++11)
io_errc(C++11)
 
std::basic_stringstream
Membri funzioni
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_stringstream::basic_stringstream
basic_stringstream::operator=
basic_stringstream::swap
basic_stringstream::rdbuf
String operazioni
Original:
String operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_stringstream::str
 
std::basic_string<CharT,Traits,Allocator> str() const;
(1)
void str(const std::basic_string<CharT,Traits,Allocator>& new_str);
(2)
Gestisce il contenuto dell'oggetto stringa sottostante.
Original:
Manages the contents of the underlying string object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
Restituisce una copia della stringa sottostante come se chiamando rdbuf()->str().
Original:
Returns a copy of the underlying string as if by calling rdbuf()->str().
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Sostituisce il contenuto della stringa sottostante come se chiamando rdbuf()->str(new_str).
Original:
Replaces the contents of the underlying string as if by calling rdbuf()->str(new_str).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Indice

[modifica] Parametri

new_str -
nuovi contenuti della stringa sottostante
Original:
new contents of the underlying string
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifica] Valore di ritorno

1)
una copia dell'oggetto stringa sottostante.
Original:
a copy of the underlying string object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
(Nessuno)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifica] Esempio

#include <sstream>
#include <iostream>
int main()
{
    int n;
 
    std::istringstream in;  // could also use in("1 2")
    in.str("1 2");
    in >> n;
    std::cout << "after reading the first int from \"1 2\", the int is "
              << n << ", str() = \"" << in.str() << "\"\n";
 
    std::ostringstream out("1 2");
    out << 3;
    std::cout << "after writing the int '3' to output stream \"1 2\""
              << ", str() = \"" << out.str() << "\"\n";
 
    std::ostringstream ate("1 2", std::ios_base::ate);
    ate << 3;
    std::cout << "after writing the int '3' to append stream \"1 2\""
              << ", str() = \"" << ate.str() << "\"\n";
}

Output:

after reading the first int from "1 2", the int is 1, str() = "1 2"
after writing the int '3' to output stream "1 2", str() = "3 2"
after writing the int '3' to append stream "1 2", str() = "1 23"

[modifica] Vedi anche

sostituisce o ottiene una copia della stringa di caratteri associata
Original:
replaces or obtains a copy of the associated character string
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(metodo pubblico) [modifica]