将输入的一行读到string中不需要像用数组那样,考虑给多少大小的空间,这可以使得做acm题更加方便。
c++98有两个函数可以读一行到string中,如下:
istream& getline (istream& is, string& str, char delim);
istream& getline (istream& is, string& str);
例子:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name;
cout << "Please, enter your full name: ";
getline ( cin, name );
cout << "Hello, " << name << "!\n";
return 0;
}
在用vc2005调试程序时遇到以下错误:
在我的程序中出现这个错误的原因是:在stack为空的情况下调用了top()方法。vc对c++中其他类比如vector,queue应该有类似的断言,就是在结构体为空的时候进行了非法的操作会引进这个错误。