- gcc -o test.out test.c //编译一个c源文件
- g++ -o test.out test.cc //编译一个c++源文件, 其实文件名的后缀对于编译什么无所谓
- #include <map> //注意,STL头文件没有扩展名.h
- map 插入元素方法:
using namespace std
map<int, string> mapint;
mapint.insert(pair<int, string>(321, "hai"));
mapint.insert(map<int, string>::value_type(321, "hai"));
- static 成员变量属于类,不属于某个具体的对象,这就意味着,即使创建多个对象,也只为 num 分配一份内存,所有对象使用的都是这份内存中的数据。当某个对象修改了 num,也会影响到其他对象。
static 成员变量必须先初始化才能使用,否则链接错误
- fprintf(stderr, "out message") 信息只会在屏幕上显示
- snprintf(a,3,"%s",p1) 拷贝P1中前3个字符到数组a中,并在末尾自动添加'\0'
- std::time_t start_time;
start_time= time(0);
cout << ctime(&start_time) << endl;
-
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 your
x
pointer goes out of scope. -
In fact C++03 deprecates use of string literal without the
const
keyword. So the declaration should be:const char*text = "text";
-
const char * 只是说指针指向的内容不可变,但指针本身可以再赋值
-
char* the_string
: I can change thechar
to whichthe_string
points, and I can modify thechar
at which it points.const char* the_string
: I can change thechar
to whichthe_string
points, but I cannot modify thechar
at which it points.char* const the_string
: I cannot change thechar
to whichthe_string
points, but I can modify thechar
at which it points.const char* const the_string
: I cannot change thechar
to whichthe_string
points, nor can I modify thechar
at which it points -
原则上应该在所有的构造函数前加explicit关键字,当你有心利用隐式转换的时候再去解除explicit,这样可以大大减少错误的发生。
-
inline很容易理解,生命为inline的函数在编译阶段即被展开成代码,而非inline的函数采用运行时链接的方式处理
-
纯虚函数是在基类中声明的虚函数,它在基类中没有定义,但要求任何派生类都要定义自己的实现方法。在基类中实现纯虚函数的方法是在函数原型后加“=0”
virtual void funtion1()=0 -
函数说明 execvp()会从PATH 环境变量所指的目录中查找符合参数file 的文件名,找到后便执行该文件,然后将第二个参数argv传给该欲执行的文件。
在ubuntu 平台上c / c++使用整理
最新推荐文章于 2023-11-20 15:37:03 发布