image.png

IDE 往往封装了 compile 和 link
image.png

std::

调用C++标准库时,要写上std::
仅仅使用非标准库文件iostream.h,则不用写

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout<<"hello world";
  6. cout<<endl;
  7. return 0;
  8. }
  1. #include <iostream>
  2. #include<fstream>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7. ofstream ofile("test.txt",ios::app);
  8. if(!ofile.fail())
  9. {
  10. cout<<"start write"<<endl;
  11. ofile<<"Mary";
  12. ofile<<"girl";
  13. ofile<<"20";
  14. }
  15. else
  16. cout<<"can not open";
  17. return 0;
  18. }
  1. #include <fstream>
  2. #include <vector>
  3. static std::vector<char> readFile(const std::string& filename) {
  4. std::ifstream file(filename, std::ios::ate | std::ios::binary);
  5. if (!file.is_open()) {
  6. throw std::runtime_error("failed to open file!");
  7. }
  8. }