随笔分类 -  C++/C 技术文章

1 2 下一页

让C程序更高效的10种方法(转)
摘要:原文:http://blog.jobbole.com/1198/代码之美,不仅在于为一个给定问题找到解决方案,而且还在代码的简单性、有效性、紧凑性和效率(内存)。代码设计比实际执行更难 。因此,每一个程序员当用C语言编程时,都应该记着这些东西。本文向你介绍规范你的C代码的10种方法。0. 避免不必要的函数调用考虑下面的2个函数:void str_print( char *str ){ int i; for ( i = 0; i < strlen ( str ); i++ ) { printf("%c",str[ i ] ); }}void str_print1 ( c 阅读全文

posted @ 2013-09-11 19:25 铁树银花 阅读(348) 评论(0) 推荐(0)

STL之next_permutation——求全排列
摘要:next_permutation功能:将[first, last)范围内的排列重组为字典序更大的下一个新排列。permutation正是“排列”之意。调用形式:next_permutation(first, last),其中,first是指向排列头元素的指针,last是指向排列末元素再下一位的指针,两者给出排列范围:[first, last).函数所在头文件:<algorithm>例子: 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 int main() 6 { 阅读全文

posted @ 2013-03-22 16:37 铁树银花 阅读(262) 评论(0) 推荐(0)

C++ pair(对组)用法
摘要:类模板:template <class T1, class T2> struct pair参数:T1是第一个值的数据类型,T2是第二个值的数据类型。功能:pair将一对值组合成一个值,这一对值可以具有不同的数据类型(T1和T2),两个值可以分别用pair的两个公有函数first和second访问。具体用法:1.定义(构造):1 pair<int, double> p1; //使用默认构造函数2 pair<int, double> p2(1, 2.4); //用给定值初始化3 pair<int, double> p3(p2); //拷贝构造函数2. 阅读全文

posted @ 2013-03-10 16:30 铁树银花 阅读(31903) 评论(2) 推荐(5)

gets()
摘要:参考资料:点击打开链接原型:char * gets ( char * str );功能:通过标准输入(stdin)读入字符并存储到C类型的字符串,当检测到换行符或者文件结束符时停止读入。换行符和文件结束符不读入字符串中。'\0'自动添加到字符串的最后。参数str:str是指向一段内存空间的指针或者是字符数组的数组名,它指向所读入的字符串,注意str指向的内存空间中原有的内容将被修改。返回值:(1)读入成功:返回str。(2)读入失败:返回NULL。例程:/* gets example */ #include <stdio.h> int main() { char s 阅读全文

posted @ 2013-01-12 15:31 铁树银花 阅读(264) 评论(0) 推荐(0)

vc6.0执行程序正确而debug版和release版运行错误
摘要:使用vc6.0直接执行程序和执行debug版本或release版本的程序在执行环境上有差异。vc6中调试运行时,默认的当前目录是工程.dsw文件所在的目录,而debug版或release版直接运行时,当前目录是exe所在的目录。比如有文件1.txt在dsw文件所在目录下而不在exe文件所在目录下,则用vc直接运行程序时能打开1.txt,但直接运行exe文件时打开失败。 阅读全文

posted @ 2013-01-08 20:49 铁树银花 阅读(249) 评论(0) 推荐(0)

convert 'std::vector<>::iterator {aka __gnu_cxx::__normal_iterator<*, std::vector<> >}' to '*' in in
摘要:错误程序:#include <iostream> #include <vector> using namespace std; struct A { int x; A(int y) {x = y;} }; int main() { A a(11217); vector<A> V; V.push_back(a); vector<A>::iterator it = V.begin(); A *p = it; return 0; }编译报错:error: cannot convert 'std::vector<A>::iterat. 阅读全文

posted @ 2012-12-19 18:47 铁树银花 阅读(1816) 评论(0) 推荐(0)

error: creating array of references( declaration of 'a' as array)
摘要:错误程序:#include <iostream> using namespace std; void func(int& a[], int n) { for(int i = 0; i < n; i++) a[i]++; } int main() { int a[3] = {1, 2, 3}; func(a, 3); cout << a[0] << ' ' << a[1] << ' ' << a[2] << endl; return 0; } 初衷:通过建立引用型形参 阅读全文

posted @ 2012-12-19 17:02 铁树银花 阅读(343) 评论(0) 推荐(0)

error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
摘要:string filename = "1.txt"; ifstream fin; fin.open(filename); 上述语句会产生如下错误:error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)原因是C++的string类无法作为open的参数。解决方案:使用C的字符串。例: char filename[10]; strcpy(filename, "1.txt"); ifstream fin; fin.o 阅读全文

posted @ 2012-12-15 23:23 铁树银花 阅读(5791) 评论(0) 推荐(0)

C++ istream::peek()
摘要:功能:peek函数用于读取并返回下一个字符,但并不提取该字符到输入流中,也就是说,依然让该字符作为将要提取到输入流的下一个字符。例程:#include <iostream> #include <string> using namespace std; int main() { string word; char c; int n; cout << "Please enter a word or a number: "; c = cin.peek(); if(isdigit(c)) { cin >> n; ... 阅读全文

posted @ 2012-12-08 09:38 铁树银花 阅读(1028) 评论(0) 推荐(0)

C/C++文件之eof()
摘要:C/C++文件之eof()版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://tuhao.blogbus.com/logs/21306687.html在使用C/C++读文件的时候,一定都使用过eof()这个函数来判断文件是否为空或者是否读到文件结尾了,也会在使用这个函数的过程中遇到一些问题,如不能准确的判断是否为空或者是否到了文件尾,以至于有些人可能还会怀疑这个函数是不是本身在设计上就有问题。先来看看如下这段代码:#include <iostream> #include <fstream> using namespace std; int ma 阅读全文

posted @ 2012-12-08 09:18 铁树银花 阅读(431) 评论(0) 推荐(0)

赋值和拷贝构造函数
摘要:有程序: 1 #include <iostream> 2 3 using namespace std; 4 5 class ClassA 6 { 7 public: 8 ClassA(int i = 0) {x = i;} 9 ClassA(const ClassA& t)10 {11 x = t.x;12 cout << "调用拷贝构造函数" << endl;13 }14 ClassA& operator = (const ClassA& t)15 ... 阅读全文

posted @ 2012-12-05 23:38 铁树银花 阅读(231) 评论(0) 推荐(0)

fopen C++
摘要:fopen<cstdio>FILE * fopen ( const char * filename, const char * mode );Open fileOpens the file whose name is specified in the parameter filename and associates it with a stream that can be identified in future operations by theFILE pointer returned.The operations that are allowed on the stream 阅读全文

posted @ 2012-12-04 18:47 铁树银花 阅读(1250) 评论(0) 推荐(0)

如何利用迭代器获取vector的最后一个元素
摘要:错误观点:通过vector::end()能获取指向最后一个元素的指针。实际上,通过上面的方法获取的是指向末尾元素再下一个位置的指针。例子:#include <iostream> #include <vector> using namespace std; int main() { vector<int> Int; Int.push_back(1); Int.push_back(5); vector<int>::iterator it = Int.end(); cout << *it << endl; return 0; } 阅读全文

posted @ 2012-11-21 20:36 铁树银花 阅读(7782) 评论(0) 推荐(0)

一个文件读写的简易例子
摘要:先在储存工程文件的文件夹中新建两个文件:in.txt和out.txt,分别用于存放需要读入的内容和写进的内容。例如在in.txt中输入a 1v 3.2d 0.2q 0w 12f 0.10 0运行程序 1 #include <fstream> 2 using namespace std; 3 4 int main() 5 { 6 ifstream in; //输入流对象 7 ofstream out; //输出流对象 8 char ch; 9 double w;10 in.open("in.txt"); //打开相应文件11 out.o... 阅读全文

posted @ 2012-11-21 10:09 铁树银花 阅读(212) 评论(0) 推荐(0)

error LNK2001: unresolved external symbol _main
摘要:Win32 控制台应用程序需要 main 函数作为入口点。当链接器在附加到项目的任何文件中都找不到 main 函数时会返回此错误消息。加上main()函数即可 阅读全文

posted @ 2012-11-11 23:38 铁树银花 阅读(113) 评论(0) 推荐(0)

c++ vector使用下标赋值出错
摘要:有以下程序#include <iostream> #include <vector> using namespace std; int main() { vector<int> v; v[0] = 1; return 0; } 运行会出现内存非法操作的错误。症结在于“v[0] = 1”一句。一开始vector为空时,不能对其进行下标赋值。而要用push_back().以下程序#include <iostream> #include <vector> using namespace std; int main() { vector< 阅读全文

posted @ 2012-11-11 17:17 铁树银花 阅读(540) 评论(0) 推荐(0)

MFC程序出现“Debug Assertion Failed! File:afx.inl Line:177”错误
摘要:程序运行时弹出提示框原因:数组访问越界。 阅读全文

posted @ 2012-11-07 10:04 铁树银花 阅读(528) 评论(0) 推荐(0)

MFC程序出现“Debug Assertion Failed! File:dlgdata.cpp Line: 43 ”错误
摘要:运行程序时,弹出提示框出错的原因是,我删掉了一个编辑框,但是没有清除相应对话框类(***Dlg.h)和资源头文件(Resource.h)中的相关信息。我删除的编辑框的映射变量为“m_strOverflow”,在所有文件中查找这一关键词,注释掉相关语句,程序就能正常运行。 阅读全文

posted @ 2012-11-07 09:36 铁树银花 阅读(428) 评论(0) 推荐(0)

C++重载运算符
摘要:本文主要讲述加号运算符“+”,自增运算符“++”,流提取运算符运“>>”,流插入运算符"<<"的重载。先给出Vector类:class Vector { public: Vector(double a = 0, double b = 0) {x = a, y = b;} //构造函数 Vector(const Vector& v){x = v.x, y = v.y;} //拷贝构造函数 Vector operator + (const Vector& v); //重载+ Vector operator ++ (); ... 阅读全文

posted @ 2012-11-05 20:27 铁树银花 阅读(466) 评论(0) 推荐(1)

MFC无法给组合框控件添加CString类成员变量
摘要:今天出现了这个问题,网上搜了很久,虽然没有找到直接的答案,但是获得了启发,解决了这个问题——其实只是自己刚入门所识有限。我原来设定组合框的类型为Drop List,下拉列表式组合框的编辑框是不能编辑的,因而不能添加字符串类,改为允许编辑的Dropdown即可。顺便介绍一下几种类型的组合框:1.简易组合框(Simple)简易组合框中的列表框是一直显示的,编辑框可以编辑。2.下拉式组合框(Dropdown)下拉式组合框默认不显示列表框,只有在点击了编辑框右侧的下拉箭头才会弹出列表框,编辑框可以编辑。3.下拉列表式组合框(Drop List)下拉列表式组合框的编辑框是不能编辑的,只能由用户在下拉列表 阅读全文

posted @ 2012-11-04 16:08 铁树银花 阅读(435) 评论(0) 推荐(0)

1 2 下一页

导航