在ubuntu 平台上c / c++使用整理

来源:互联网 发布:eia数据汇总 编辑:程序博客网 时间:2024/06/05 16:00
  1. gcc -o test.out test.c //编译一个c源文件
  2. g++ -o test.out test.cc //编译一个c++源文件, 其实文件名的后缀对于编译什么无所谓
  3. #include <map> //注意,STL头文件没有扩展名.h
  4. map 插入元素方法:
    using namespace std
    map<int, string> mapint; 
    mapint.insert(pair<int, string>(321, "hai"));
    mapint.insert(map<int, string>::value_type(321, "hai"));
  5. static 成员变量属于类,不属于某个具体的对象,这就意味着,即使创建多个对象,也只为 num 分配一份内存,所有对象使用的都是这份内存中的数据。当某个对象修改了 num,也会影响到其他对象。
    static 成员变量必须先初始化才能使用,否则链接错误
  6. fprintf(stderr, "out message") 信息只会在屏幕上显示
  7. snprintf(a,3,"%s",p1)  拷贝P1中前3个字符到数组a中,并在末尾自动添加'\0'
  8.     std::time_t start_time;
        start_time= time(0);
        cout << ctime(&start_time) << endl;
  9. When you instantiate object with automatic storage duration, like this (where X is some class):

    X x;

    You are creating an object which will be automatically destroyed when it goes out of scope. On the other hand, when you do:

    X* x = new X();

    You are creating an object dynamically and you are binding its address to a pointer. This way, the object you created willnot be destroyed when yourx pointer goes out of scope.

  10. In fact C++03 deprecates use of string literal without theconst keyword. So the declaration should be:

    const char*text = "text";
  11. const char * 只是说指针指向的内容不可变,但指针本身可以再赋值
  12. char* the_string : I can change the char to which the_string points, and I can modify the char at which it points.

    const char* the_string : I can change the char to which the_string points, but I cannot modify the char at which it points.

    char* const the_string : I cannot change the char to which the_string points, but I can modify the char at which it points.

    const char* const the_string : I cannot change the char to which the_string points, nor can I modify the char at which it points

  13. 原则上应该在所有的构造函数前加explicit关键字,当你有心利用隐式转换的时候再去解除explicit,这样可以大大减少错误的发生。
  14. inline很容易理解,生命为inline的函数在编译阶段即被展开成代码,而非inline的函数采用运行时链接的方式处理
  15. 纯虚函数是在基类中声明的虚函数,它在基类中没有定义,但要求任何派生类都要定义自己的实现方法。在基类中实现纯虚函数的方法是在函数原型后加“=0”
     virtual void funtion1()=0
  16. 函数说明 execvp()会从PATH 环境变量所指的目录中查找符合参数file 的文件名,找到后便执行该文件,然后将第二个参数argv传给该欲执行的文件。
0 0
原创粉丝点击