
C/CPP学习
文章平均质量分 55
Just_Nothing_at_all
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
对于 sizeof(class_name) 值的讨论
之前遇到过一个问题, 说是 sizeof(class_name) 的值是多少, 不解.But, 在看了 > 有所理解之后, 对于这个问题我得到了似乎正确的答案, 现总结如下, 如有不妥, 望请斧正(虽说我觉得几乎没人会看我的博客吧......= =)对了, 要求严肃的朋友请直接跳过括号内容, 嗯......对于一个 class A, sizeof(A) 是什么意思呢?原创 2014-11-07 01:58:15 · 483 阅读 · 0 评论 -
找出两串有序数组中相同的元素, 并存入新数组中的目前最快方法
#include #include using namespace std;int main (){ std::ios::sync_with_stdio (false); setiset1{ 1, 3, 5, 7, 9, 11 }; setiset2{ 1, 2, 3, 4, 5, 6, 8, 10 }; setsame; auto p1 = iset1原创 2014-08-15 13:53:46 · 984 阅读 · 0 评论 -
override 说明符的用法
/* 派生类若定义了一个与基类虚函数名字相同形参列表不同的函数, 这仍然是合法的 但派生类中的函数并没有覆盖掉基类中的虚函数 因此可能导致莫名的错误 为避免此类问题, 可在派生类版本的形参列表 || const 后添加override 说明符 由此来保证代码的行为符合预期*/struct Base{ virtual void原创 2014-08-17 01:00:54 · 393 阅读 · 0 评论 -
设计模式-装饰者模式
动态地将 #include #include //drinkclass Beverage{public: Beverage () = default; Beverage (string description_) :description (description_) {} virtual ~Beverage () = default; virt原创 2014-09-22 17:47:26 · 267 阅读 · 0 评论 -
运算符重载
class Foo{public: Foo () :a (1) {} int get_value () const{ return a; }private: int a;};ostream &operator<<(ostream &os, const Foo &f){ os << "the Foo's value is: " << f.get_value原创 2014-08-12 00:54:29 · 321 阅读 · 0 评论 -
设计模式: 观察者模式
#include #include #include using namespace std;class Observer{ friend class weather_data;public: Observer (size_t ID_) :ID (ID_) {} virtual ~Observer () = default;protected: si原创 2014-09-22 02:48:36 · 315 阅读 · 0 评论 -
Qt 与C11 对string 的处理
#include #include #include using namespace std;QTextStream cin(stdin, QIODevice::ReadOnly);QTextStream cout(stdout, QIODevice::WriteOnly);void display_data(char *buff, size_t len){原创 2014-09-20 16:27:50 · 752 阅读 · 0 评论 -
对象引用前加const 报错
/* error: 不能将"this"指针从"const Foo"转换为"Foo &" class Foo { public: Foo () :a (1) {} int get_value () { return a; } private: int a; }; ostream &opera原创 2014-08-11 23:53:46 · 590 阅读 · 0 评论 -
输出流的格式控制
//bool 值的格式 cout << "default bool format: " << true << " " << false << "\n" << "alpha bool values: " << boolalpha << true << " " << false << "\n" << "original bool format: " << noboolalpha <<原创 2014-09-07 19:03:13 · 382 阅读 · 0 评论 -
重载模板的顺序
重载 #include using namespace std;template void g (T t) { cout << "g(T)" << " "; } template void g (T *t) { cout << "g(T*)" << " "; } //匹配所有*p 实参, 即使是co原创 2014-09-05 18:33:24 · 365 阅读 · 0 评论 -
支持逻辑运算符的文本查询程序的OOP实现
#include #include #include #include #include #include #include #include #include using namespace std;//All of classes are pointerclass QueryResult{ friend ostream& p原创 2014-08-20 15:15:02 · 293 阅读 · 0 评论 -
一个类似vector的类
#include #include #include #include #include using namespace std;class StrVec{public: StrVec () : elements (nullptr), first_free (nullptr), cap (nullptr) {}; //add a constru原创 2014-08-08 15:59:17 · 445 阅读 · 0 评论 -
模版实参的推断和引用
//从左值引用函数参数推断类型//只能传左值, 若实参为const, 则推断为const 左值引用template void f1(T&);int i = 0;const int ci = i;f1(i); //T is intf1(ci); //T is const intf1(5); //error原创 2014-09-05 00:11:06 · 392 阅读 · 0 评论 -
拷贝初始化的几种情况
#include #include using namespace std;class Point{public: Point () = default; Point (const Point&) { cout << "Hey" << endl; }};Point foo_bar (Point arg)//1{ Point global; P原创 2014-07-31 22:39:17 · 337 阅读 · 0 评论 -
C++实现的2048(转)
转载来源:http://blog.youkuaiyun.com/iaccepted/article/details/38300107转载 2014-07-30 20:42:26 · 428 阅读 · 0 评论 -
用容器储存有继承关系的对象
/* 若容器中直接储存类(如vector),则当push_back一个 D 时,其派生部分会被"切掉". 因此在用容器储存带有继承关系的对象时, 我们将选用基类的指针(最好是智能指针). 目的是保证容器的兼容性.*/#include #include #include using namespace std;class B{pu原创 2014-08-17 21:59:37 · 435 阅读 · 0 评论 -
简单模版的使用
#include #include #include #include //accept an array, print it's elemstemplatevoid Print (const T (&array)[M]){ for (const auto &elem : array) cout << elem << "\n";}//accep原创 2014-09-01 07:43:20 · 285 阅读 · 0 评论 -
设计模式-单例模式
#include #include using namespace std;class Single{public: Single (const Single &) = delete; Single &operator=(const Single&) = delete; static shared_ptr get_instance () { if (u原创 2014-09-24 01:35:47 · 311 阅读 · 0 评论 -
STL 标准容器的选择
1. 除非有很好的理由选择其他容器, 否则应使用 vector.2.如果有很多小的元素, 且空间的额外开销很重要, 则不要使用 list 或 forward_list.3.如果程序要求随机访问元素, 应使用 vector 或 list.4.如果程序需要在头尾 插入/删除 元素,且不会在中间插入元素, 则使用 deque5.如果需要在中间插入元素, 应使用 list 或 forwar原创 2014-11-05 21:49:56 · 360 阅读 · 0 评论 -
插入排序实现
templatevoid InsertSort (Container &container){ for (int i = 1; i < container.size (); ++i) for (int j = i; j > 0 && container[j] < container[j - 1]; --j) swap (container[j], container[j原创 2014-11-03 23:21:10 · 284 阅读 · 0 评论 -
选择排序实现
templatevoid SelectionSort (Container &container){ for (auto i = container.begin (); i != container.cend (); ++i) { auto min = i; for (auto j = i + 1; j != container.cend (); ++j) if (原创 2014-11-03 23:19:37 · 303 阅读 · 0 评论 -
希尔排序实现(不太满意)
templatevoid ShellSort (Container &container){ const int container_len = container.size (); int sub_container_len = 1; //设置合适的sub_container_len while (sub_container_len < container_len / 3原创 2014-11-03 23:23:51 · 361 阅读 · 0 评论 -
接受一个&istream参数,打印在标准输出上
此函数从给定流中读取数据, 直至遇到文件结束标识时停止, 他将读取的数据打印在标准输出上, 完成这些之后, 在返回流之前对其进行复位, 使其处于有效状态.istream& ReadWordsFormConsole (istream & input){ stringword; vectorwords; while (input>> word || !input.e原创 2014-07-18 21:07:18 · 874 阅读 · 0 评论 -
Default Constructor 的建构操作
default constructor 会在需要的时候被编译器产生出来 ---->那么问题来了, 是在谁需要的时候? 考察以下代码:class Foo{public: int val; Foo *pnext};void FooBar(){//Foo Object 须在此初始化Foo bar;if(bar.val || bar.pnext)//do原创 2014-11-14 14:42:54 · 458 阅读 · 0 评论 -
C++ 中可确定的内置类型的大小关系
1 ≡ sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long)• 1 ≤ sizeof(bool) ≤ sizeof(long)• sizeof(char) ≤ sizeof(wchar_t) ≤ sizeof(long)• sizeof(float) ≤ siz原创 2014-11-11 18:41:29 · 456 阅读 · 0 评论 -
TC++PL's Advices(1)
全为自己翻译, 如有不妥, 望请斧正.Programming In C++:[1] Represent ideas directly in code.[2] Represent relationships among ideas directly in code (e.g., hierarchical, parametric, andownership relationship原创 2014-11-11 22:54:37 · 495 阅读 · 0 评论 -
C++ 零散知识
全局变量的初始化时机: C 是在编译期初始化, C++ 是在程序运行时, main 函数之前初始化.不使用中间变量实现 strlen:int MyStrlen (const char *str){return *str? MyStrlen(++str) + 1 : 0;}C 语言函数参数入栈顺序为从右至左。具体原因为:C 方式参数入栈顺序(从右至左)的好处就是可以动原创 2014-11-11 13:19:39 · 340 阅读 · 0 评论 -
类型分类技术(type classification)
/* *设一个模版具有模版参数T, 表示C++ 中的某种类型 * 有些情况下随着T 的不同, 模版会做不同的处理 * 所以需要了解T 的具体信息 * * 我们可以用类型分类技术(type classification)提供T 的信息 * 我们将所被指的或被引用的类型命名为baseT * 将T 最终涉及的C++ 基本类型设为bot原创 2014-09-17 01:15:45 · 619 阅读 · 0 评论 -
Traits技术
/* *Traits技术以一个统一的编程接口描述各种数据类型的基本特征. * *例如float 的最大值为 2^128, 在float.h 中被定义为FLT_MAX_EXP * double 的最大值为 2^1024,被定义为DBL_MAX_EXP. * 此外, float 和double 的最小值也不同, 分别定义为 * FLT_EPS原创 2014-09-17 00:06:36 · 301 阅读 · 0 评论 -
设计模式: 策略模式
设有一基类Duck, 为原创 2014-09-16 02:01:23 · 348 阅读 · 0 评论 -
斐波那契数列规律的解释及实现
很多人都听说过斐波那契数列, 也知道它的规律, 但有些人原创 2014-09-03 01:57:09 · 1095 阅读 · 0 评论 -
C++ 多线程入门1
#include #include using namespace std;struct my_thread{ my_thread (int i_) :i (i_) {} void operator()() { //do_something; }private: int i;};//using RAIIclass thread_gu原创 2014-09-14 19:38:00 · 375 阅读 · 0 评论 -
读取文件, 统计字符,忽略大小写
int main (int argc, char* argv[]){ std::ios::sync_with_stdio (false); ifstreamfile; file.open(argv[1]); mapword_count; string word,line; while(getline (file, line)) {原创 2014-07-18 21:07:35 · 397 阅读 · 0 评论 -
不用加减乘除做加法
引用地址: http://blog.youkuaiyun.com/caroline_wendy题目: 写一个函数,求两个整数之和, 要求在函数体内不得使用+, -, *, /四则运算符号.不能使用运算符号,使用位运算,第一步异或运算选位,第二步与运算进位.代码:[cpp] view plaincopy代码(C)" />代码(C)" /> #include #原创 2014-07-18 21:07:41 · 439 阅读 · 0 评论 -
使用流迭代器, sort, co…
int main (){ cout //定义输入流迭代器和尾后迭代器 istream_iterator input_number_it (cin),eof; //用迭代器与尾后迭代器初始化vector vectornumbers(input_number_it, eof); sort(numbers.begin (), numbers.e原创 2014-07-18 21:07:22 · 489 阅读 · 0 评论 -
简单的读写文件
int main (int argc, char *argv[]){ string file= argv[1] ; cout ofstream out(file, ofstream::app); stringword; out <<endl; while (cin>> word) out ifstream i原创 2014-07-18 21:07:14 · 271 阅读 · 0 评论 -
删除容器中重复字符串并按长度排序…
#include >#include >#include >#include >using namespace std;string make_plural(size_t ctr, const string &word, const string&ending){ return (ctr> 1) ? word + ending : word;}bo原创 2014-07-18 21:07:08 · 447 阅读 · 0 评论 -
输入一串字符, 统计特定字符个数
include include include int main(int argc, char **argv){ int a = 0, b = 0, c = 0; string str; getline(cin, str); for(auto it = str.begin(); it != str.end(); ++it){原创 2014-07-18 21:07:06 · 1282 阅读 · 0 评论 -
返回数组指针的函数
int array[10];int (*func(int i))[10];using arrInt = int[10];arrInt *func2(int i);typedef int arrayInt[10];arrayInt *func3(int i);auto func4 (int i) -> int(*)[10];decltype(array) *func5(int原创 2014-07-18 21:06:57 · 276 阅读 · 0 评论 -
mian函数接受两个实参, 连成s…
#include"iostream"int main(int argc, char **argv) { std::stringstr; for (size_ti = 0; argv[i]; ++i) str += argv[i]; std::cout return0;}原创 2014-07-18 21:06:55 · 437 阅读 · 0 评论