
C++
文章平均质量分 70
Restart_2013
这个作者很懒,什么都没留下…
展开
-
C/C++语言中extern的用法
在C语言中,修饰符extern用在变量或者函数的声明前,用来说明“此变量/函数是在别处定义的,要在此处引用”。1. extern修饰变量的声明。举例来说,如果文件a.c需要引用b.c中变量int v,就可以在a.c中声明extern int v,然后就可以引用变量v。这里需要注意的是,被引用的变量v的链接属性必须是外链接(external)的,也就是说a.c要引用到v,不只是取决于在a转载 2013-06-17 09:44:46 · 530 阅读 · 0 评论 -
namespace的用法
C++中采用的是单一的全局变量命名空间。在这单一的空间中,如果有两个变量或函数的名字完全相同,就会出现冲突。当然,你也可以使用不同的名字,但有时我们并不知道另一个变量也使用完全相同的名字;有时为了程序的方便,必需使用同一名字。比如你定义了一个变量string user_name, 有可能在你调用的某个库文件或另外的程序代码中也定义了相同名字的变量,这就会出现冲突。命名空间就是为解决C++中的变转载 2013-06-17 11:48:36 · 645 阅读 · 0 评论 -
istringstream对象可以绑定一行字符串,然后以空格为分隔符把改行分隔开来
#include #include #include #include using namespace std;void TestIstringStream(void){ string word, line; getline(cin, line); istringstream isstream(line); while (isstream >> word) { c原创 2013-06-26 15:40:39 · 661 阅读 · 0 评论 -
C++标准库find简单用法
#include #include #include //注意要包含该头文件using namespace std;//例一,在数组中查找int main(){ int nums[] = { 3, 1, 4, 1, 5, 9 }; int num_to_find = 5; int start = 0; int end = 6; int* result = find( nums转载 2013-06-26 20:41:49 · 856 阅读 · 0 评论 -
map和piair的几种用法
#include #include #include #include #include #include using namespace std;int main(){ pair sipr; string str("hello"); int var = 1; vector > pvec; /* // 方法1 sipr = make_pair(str, v原创 2013-06-26 22:25:52 · 687 阅读 · 0 评论 -
static_cast 的部分用法
#include #include #include #include #include #include #include using namespace std;class CBaseX{public: int x; CBaseX() { x = 10; } void foo() {转载 2013-06-27 12:38:13 · 805 阅读 · 0 评论 -
我对C++函数重载的一点小认识
// 正确:参数类型不同,返回值类型相同class A{public: int fun(void) { } int fun(int) { }};/*// 错误:参数类型不能相等class B{public: void fun(int) { } int fun(int) { }};*/// 正确:返回值类型可以不同,参数类型必须不同cla原创 2013-06-27 15:35:11 · 469 阅读 · 0 评论 -
C++拷贝构造函数(深拷贝,浅拷贝)
C++拷贝构造函数(深拷贝,浅拷贝)对于普通类型的对象来说,它们之间的复制是很简单的,例如:int a=88;int b=a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量。下面看一个类对象拷贝的简单例子。 #include using namespace std;class CExample {private: int a;public转载 2013-07-05 10:09:59 · 575 阅读 · 1 评论 -
C++创建一个类(1.只能在堆上创建; 2 只能在栈上创建; 3 不能被继承(堆上和栈上都可以创建))
/*1 只能在堆上创建对象(不能被继承,不能再栈上创建对象) 将类的构造函数声明为private,但是为了 创建该类的对象,则必须提供创建对象和释放对象的接口, 用static函数成员实现。 #include using namespace std; class HeapOnly { public: static HeapOnly *Create原创 2013-09-05 12:15:34 · 967 阅读 · 0 评论