cin的问题??
近日在读侯捷的Essential C++,
对第一章的习题有些疑问?
请问如下代码怎样结束输入:
#include <vector>
#include <string>
using namespace std;
void ex1_xtra1()
{
vector< string* > sp_vec;
string st;
cout << "Please enter a series of strings\n";
while ( cin >> st ) //死循环??????
sp_vec.push_back( new string( st ));
vector<string*>::iterator
iter = sp_vec.begin(),
it_end = sp_vec.end();
cout << endl;
cout << "This is what you entered, no?\n";
for ( int items = 1; iter != it_end; ++items, ++iter ) {
if ( !( items % 8 ))
// print a newline every 8th item
cout << '\n';
// *iter returns a string*; (*(*iter)) yields string
cout << (*( *iter )) << ' ';
delete *iter;
}
cout << endl;
}
int main() {
ex1_xtra1();
return 0; // quiets vc++
}
另外,对
int i
cin<<i
若输入字符型数据,会导致该段代码下的其它cin无效,该如何解决?