string类定义在STL中MSDN例: // string_getline_sample.cpp // compile with: /EHsc // Illustrates how to use the getline function to read a // line of text from the keyboard. // // Functions: // // getline Returns a string from the input stream. //////////////////////////////////////////////////////////////////////#pragma warning(disable:4786) #include <string> #include <iostream>using namespace std ;int main() { string s1; cout << "Enter a sentence (use <space> as the delimiter): "; getline(cin,s1, ' '); cout << "You entered: " << s1 << endl;; }
谢谢!但是,如果没有 using namespace std 那么编译的时候就会出现:文件string 找不到的错误提示。为什么会这样?
// string_getline_sample.cpp
// compile with: /EHsc
// Illustrates how to use the getline function to read a
// line of text from the keyboard.
//
// Functions:
//
// getline Returns a string from the input stream.
//////////////////////////////////////////////////////////////////////#pragma warning(disable:4786)
#include <string>
#include <iostream>using namespace std ;int main()
{
string s1;
cout << "Enter a sentence (use <space> as the delimiter): ";
getline(cin,s1, ' ');
cout << "You entered: " << s1 << endl;;
}
是指使用旧的C式库,它只是为了与旧的C编译器兼容才保留,将来会按新的C++标准去掉#include <string>
指用新C++标准的模板类库(STL),是新的C++程序应当立刻采用的,除非为了兼容旧编译器而不采用
那么c++primer里面的头文件都能被正确引用