#include<iostream>
#include<string>
#include<cstring>
using namespace std;
void copy(string & n);
int main()
{
cout<<“Enter a string(q to quit):”<<endl;
string str;
getline(cin,str);
while(str!=”q”)
{
copy(str);
cout<<str<<endl<<“Enter a string(q to quit):”<<endl;
getline(cin,str);
}
return 0;
}
void copy(string & n)
{
int i=0;
while(n[i])
n[i]=toupper(n[i++]);
}
2
10
void copy(string & n) { int i = 0; while (i < n.length()) { n[i] = toupper(n[i]); ++i; } }
5
10
2
4
改成:
for (i = 0; i < n.size(); ++i) n[i]=toupper(n[i]);
15
function template
<string> std::getline (basic_string)(1) template <class charT, class traits, class Alloc>
basic_istream<charT,traits>& getline (basic_istream<charT,traits>& is,
basic_string<charT,traits,Alloc>& str, charT delim);
(2) template <class charT, class traits, class Alloc>
basic_istream<charT,traits>& getline (basic_istream<charT,traits>& is,
basic_string<charT,traits,Alloc>& str);
(1) template <class charT, class traits, class Alloc>
basic_istream<charT,traits>& getline (basic_istream<charT,traits>& is,
basic_string<charT,traits,Alloc>& str, charT delim);
template <class charT, class traits, class Alloc>
basic_istream<charT,traits>& getline (basic_istream<charT,traits>&& is,
basic_string<charT,traits,Alloc>& str, charT delim);
(2) template <class charT, class traits, class Alloc>
basic_istream<charT,traits>& getline (basic_istream<charT,traits>& is,
basic_string<charT,traits,Alloc>& str);
template <class charT, class traits, class Alloc>
basic_istream<charT,traits>& getline (basic_istream<charT,traits>&& is,
basic_string<charT,traits,Alloc>& str);
Get line from stream into string
Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, for (2)).
The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.
Each extracted character is appended to the basic_string as if its member push_back was called.
Parameters
is
basic_istream object from which characters are extracted.
str
basic_string object where the extracted line is stored.
Return Value
The same as parameter is.
A call to this function may set any of the internal state flags of is if:
flag error
eofbit The end of the source of characters is reached during its operations.
failbit The input obtained could not be interpreted as a valid textual representation of an object of this type.
In this case, distr preserves the parameters and internal data it had before the call.
Notice that some eofbit cases will also set failbit.
badbit An error other than the above happened.
(see ios_base::iostate for more info on these)
Additionally, in any of these cases, if the appropriate flag has been set with is”s member function basic_ios::exceptions, an exception of type ios_base::failure is thrown.
Example
1234567891011121314 // extract to string
#include <iostream>
#include <string>
main ()
{
std::string name;
std::cout << “Please, enter your full name: “;
std::getline (std::cin,name);
std::cout << “Hello, ” << name << “!\n”;
return 0;
}
Complexity
Unspecified, but generally linear in the resulting length of str.
Iterator validity
Any iterators, pointers and references related to str may be invalidated.
Data races
Both objects, is and str, are modified.
Exception safety
Basic guarantee: if an exception is thrown, both is and str end up in a valid state.
See also
basic_istream::getlineGet line (public member function )operator>> (basic_string)Extract string from stream (function template )
9
getline自动读出一行内容,但是不包含换行