??xml version="1.0" encoding="utf-8" standalone="yes"?>const int VMAX = 1010;
typedef struct Graph
{
int vex;
Graph* next;
}Graph;
Graph ArcGraph[VMAX];
void insert(int u, int v)
{
Graph* t = new Graph;
Graph* p = ArcGraph[u].next;
t->vex = v;
t->next = p;
ArcGraph[u].next = t;
}
完成完整的程序提交上去,得到l果
Memory:25796K Time:375MS
Language:C++ Result:Accepted
再对比别人的E序
Memory:296K Time:109MS
无论是时间还是空间都相差很大?br>于是p虑怎么优化自己的程序?br>W一个问题:规模只有1000Qؓ什么会用那么多内存呢?
仔细一x据是多case的,每次插入新节Ҏ(gu)都要动态创Z个节炏V?br>一来动态创时_二来每个casel束的邻接表中的节点没有释放Q故耗费大量内存?br>然后想C下面的算法,首先初始化一块内存Graph use[100*VMAX];q样每次需要新节点Ӟ
׃use中获取。如果use使用完毕再动态创建?br>
依此法优化后,得到的结果比较满?br>Memory:1000K Time:218MS
Language:C++ Result:Acceptedconst int VMAX = 1010;
typedef struct Graph
{
int vex;
Graph* next;
}Graph;
Graph ArcGraph[VMAX];
Graph use[100*VMAX];
int size = 0;
void insert(int u, int v)
{
Graph* t;
if(size < 100*VMAX)
{
t = &use[size];
size++;
}
else t = new Graph;
Graph* p = ArcGraph[u].next;
t->vex = v;
t->next = p;
ArcGraph[u].next = t;
}
]]>#include<iostream>
#include<bitset>
using Namespace stdnamespace std;
const int n = 4;
int main()
{
for(int i = 0; i < (1 << n); i++)
{
bitset<n> bit(i);
for(int j = bit.size() - 1; j >= 0; j--)
cout<<bit[j];
cout<<endl;
}
return 0;
}
i??^n - 1,
把它换算成二q制分别对应一个子集?
]]>#include<iostream>
using Namespace stdnamespace std;
const int n = 4;
int x[n];
//回溯?br>
void backtrack(int t)
{
if(t >= n)
{
for(int i = 0; i < n; i++)
cout<<x[i];
cout<<endl;
}
else
{
for(int i = 0; i <= 1; i++)
{
x[t] = i;
backtrack(t + 1);
}
}
}
//位运?br>
void bitOperate()
{
for(int i = 0; i < (1 << n); i++)
{
for(int j = 0; j < n; j++)
{
if( (i & (1 << j) ) == 0)
x[j] = 0;
else
x[j] = 1;
}
for(int j = 0; j < n; j++)
cout<<x[j];
cout<<endl;
}
}
int main()
{
backtrack(0);
cout<<endl;
bitOperate();
return 0;
}
]]>#include<iostream>
#include<string>
using Namespace stdnamespace std;
const int MAXN = 9973; //哈希表长?br>
const int len = 30; //字符串的最大长?br>
int Htable[MAX];
char ch[MAX][len]; //存储关键字的字符?br>
unsigned long Hash(char * key)
{
unsigned long h = 0;
while(*key)
{
h = (h << 4) + *key++;
unsigned long g = h & 0xf0000000L;
if(g)
h ^= g >> 24;
h &= ~g;
}
return h % MAX;
}
int search(char * key)
{
unsigned long i = Hash(key);
while(Htable[i])
{
if(strcmp(ch[Htable[i]], key) == 0)
return i;
i = (i + 1) % MAX;
}
return -1;
}
int insert(char * key, int j) //j为关键字在ch中的位置Q即索引
{
unsigned long i = Hash(key);
while(Htable[i])
i = (i + 1) % MAX;
Htable[i] = j;
return i;
}
]]>
通过<操作W可知在整数中元素大的优先高?br>故示?中输出结果ؓQ? 6 5 3 2
W二U方法:
在示?中,如果我们要把元素从小到大输出怎么办呢Q?br>q时我们可以传入一个比较函敎ͼ使用functional.h函数对象作ؓ比较函数?/p>
其中
W二个参Cؓ容器cd?br>W二个参Cؓ比较函数?br>故示?中输出结果ؓQ? 3 5 6 9
W三U方法:
自定义优先?/p>
在该l构中,value为|priorityZ先?br>通过自定义operator<操作W来比较元素中的优先U?br>在示?中输出结果ؓQ?br>优先U?nbsp; ?br>9 5
8 2
6 1
2 3
1 4
但如果结构定义如下:
则会~译不过QG++~译器)
因ؓ标准库默认用元素类型的<操作W来定它们之间的优先关系?br>而且自定义类型的<操作W与>操作Wƈ无直接联p,故会~译不过?br>
//代码清单
scanf()函数是通用l端格式化输入函敎ͼ它从标准输入讑֤(键盘) d输入的信息。可以读入Q何固有类型的数据q自动把数值变换成适当的机内格式?/p>
其调用格式ؓ: scanf("<格式化字W串>"Q?lt;地址?gt;);
scanf()函数q回成功赋值的数据Ҏ(gu)Q出错时则返回EOF?/p>
其控制串׃cdW构成:
1。格式化说明W;
2。空白符Q?br />3。非I白W;
QAQ ?格式化说明符
格式字符 说明
%a d一个Q点?仅C99有效)
%A 同上
%c d一个字W?br />%d d十进制整?br />%i d十进Ӟ八进Ӟ十六q制整数
%o d八进制整?br />%x d十六q制整数
%X 同上
%c d一个字W?br />%s d一个字W串
%f d一个QҎ(gu)
%F 同上
%e 同上
%E 同上
%g 同上
%G 同上
%p d一个指?br />%u d一个无W号十进制整?br />%n x已读入值的{h(hun)字符?br />%[] 扫描字符集合
%% ?W号
#include<iostream> #include<sstream> using namespace std; int main() { string str, line; while(getline(cin, line)) { istringstream stream(line); while(stream>>str) cout<<str.c_str()<<endl; } return 0; }试Q?br />inputQ?br />abc df e efgeg ffg
#include<cstdlib>
#include<cstdio>
int main() { int num = 10; char str[100]; int n = atoi(itoa(num, str, 2)); printf("%d\n",n); return 0; }
先把num转换Zq制的字W串Q再把该字符串{换ؓ整数?/pre>
]]>
[Andrew Koeing & Barbara MOO] Accelerated C++
Accelerated c++, Andrew Koeing & Barbara MOO, Addison Wesley, 2000
[Eckel2000] Thinking in C++
Thinking in C++ 2/e Bruce Eckel 2000 1470 pages Prentice Hall
C++ ~程思想Q刘宗田{?译,420?
[Lippman98] C++Primer
C++ Primer,3rd Editoin,by Stanley Lippman and Josee Lajoie
Addison Wesley Longman,1998 1237 pages
C++ Primer 中文版,侯俊?译,1999Q?237?
[Struostrup2000] The C++ Programming Language
The C++ Programming Language,Special Editoin,by Bjarne Stroustrup
Addison Wesley Longman,2000,1017 pages
[ANSI C++] C++规格?1998.9.1 PDF格式
ANSI C++ 1996 Draft
层二:专家l验(C++/OOP)
[Meyers96] More Effective C++
More Effective C++, by Scott Meyers,Addison Wesley,1996,318pages
More Effective C++中文版,侯俊李ͼ培生 2000. 318?
[Meyers98] Effective C++
Effective C++, Second Edition,by Scott Meyers,Addison Wesley Longman,1998.256pages
Effective C++ 2/e 中文?侯俊?培生 2000.256?br />Effective C++, Third Edition, by Scott Meyers, Addison Wesley Longman.
[Sutter99] Exceptional C++
Exceptional C++Qby Herb Sutter,Addison Wesley Longman,2000.208pages
Exceptional C++中文版,侯俊?培生 2000.248?
[Sutter2001]More Exceptional C++
More Exceptional C++ by Herb Sutter, Addison Wesley Longman, 2001.
[Sutter2004]Exception C++ Style
Exception C++ Style by Herb Sutter, Addison Wesley Longman, 2004.
层三:底层机制(C++ Object Model)
[Ellis90] The Annotated C++ Reference Manual
The Annotated C++ Reference Manual,by Margaret A.Ellis and Bjarne Stroustrup
Addison Wesley Longman,1990,447 pages.
[Lippman96] Inside the C++ Object Model
Inside the C++ Object Model,by Stanley Lippman,Addison Wesley Longman,1996,280pages
深度探烦C++物g模型Q侯俊杰 ?
层四:设计观念的复?C++/Patterns)
[Gamma95] Design PatternsQElements of Reusable Object Oriented Software,
by Erich Gamma,Richard Helm,Ralph Johnson,and John Vlissides,Addison Wesley,1995.395pages
设计模式,李英军等?机械工业出版C?2000.254?
[Alex2001]Modern C++ Design: Generic Programming and Design Patterns Applied
by Andrei Alexandrescu,Addison-Wesley,2001,352Paper
Genericity/STLpd(与层U二同步Q?
W一个境界是使用STL:
[Josuttis99]:The C++ Standard Library QA Tutorial and Reference,by Nicolai M.Josuttis,
Addison Wesley 1999.799pages
W二个境界是了解泛型技术的内涵与STL的学?
[Austern98]:Generic Programming and the STL -Using and Extending the C++ Standard
Template library,by Matthew H.Austern,Addison Wesley 1998.548page
W三个境界是扩充STL:
[Stepanov2001]:C++ Standard Template Library by P.J.Plauger,Alexander A.Stepanov,
Meng Lee,David R.Musser,Prentice Hall 2001
其他书目Q?br />1. Large-scale C++ software Design, John Lako, Addison Wesley, 1996
2. Effective STL, Scott Meyers, Addison Wesley, 1995
3. C++ FAQs, 2nd, Marshall Cline, Greg Lomow, Mike Girou, Addison Wesley, 1998
4. C++ Gotchas, Stephen Dewhurst, Addison Wesley, 2002
5. C++ templates, the complete Guide, Daveed Vandevoorde & Nicolar M.Josuttis, Addison Wesley, 2002
6. Standard C++ iostreams and Locals, Angelika Langer & Klaus Kreft, Addison Wesley, 2000
7. Design & Evolution of C++, BS, Addison Wesley, 1994
8. Modern C++ Design, Andrie Alexandrescu, Addison Wesley, 2001
9. Generative Programming, Krzysztof Czarnecki & Ulrich Eisencecker, Addison Wesley, 2000
10.Pattern-oriented software architecture, Vol1:A system of patterns, Frank Buschmann, 1996
11. STL 源码剖析Q侯?br />12. C++ Coding Standards 101 Rules Guidelines, Andrie Alexandrescu & Herb Sutter, Addison Wesley, 2005
find.h #ifndef FIND_H #define FIND_H template <typename ITER , typename T> ITER find(ITER begin, ITER end, T value) { ITER it; for(it = begin; it != end; ++it) if(*it == value) break; return it; } #endif
count.h #ifndef COUNT_H #define COUNT_H template <typename ITER , typename T> int count(ITER begin, ITER end, T value) { int sum = 0; for(ITER it = begin; it != end; ++it) { if(*it == value) ++sum; } return sum; } #endif
以后接着写?/pre>
]]>
插入|删除位置
/效率
|
头部
|
中间
|
N
|
随机讉K
|
vector
|
?/font>
|
?/font>
|
?/font>
|
Yes
|
list
|
?/font>
|
?/font>
|
?/font>
|
No
|
deque
|
?/font>
|
?/font>
|
?/font>
|
Yes
|
q里以stringcMؓ例:
1、string str1 = "";
2、string str2 = string();
3、string str3;
W一U方式:调用stringcL受字W串形参的构造函敎ͼ创徏一个(f)时对象?br />然后调用复制构造函数用该对象初始化str1?/font>
W二U方式:调用stringcȝ默认构造函敎ͼ创徏一个(f)时对象?br />然后调用复制构造函数用该对象初始化str2?/font>
W三U方式:直接调用stringcȝ默认构造函数初始化str3?/font>
创徏临时对象的代h高的?/font>
因此创徏对象应尽量不要用会创徏临时对象的初始化方式?/font>
在用类时也量不要创徏临时对象?/font>
它能自动识别各种关键字,pȝ函数Q成员变量,
自动l出输入提示Q自动更正大写错误Q?
自动标示错误Q等{等{?
用了它,你写代码的速度一定大大提高?
看到一D代码:
char *p1,*p2;
//something
if(p1!=0)strcpy(p2,p1);
奇怪,指针怎么能够?相比呢?
N days later...
一个有效的指针有三U状态:
一、保存一个特定对象的地址?
二、指向某个对象后面的另一对象?
三、gؓ0Q表明它不指向Q何对象?
if(p1!=0)strcpy(p2,p1);
<=>
if(p1!=NULL)strcpy(p2,p1);// q种形式更易理解?
NULL 是在 cstdlib 头文件中定义的变量,gؓ0.
操作不存在的元素Q如:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str[0];
cout<<str[0];
return 0;
}
q样得到输出的结果是不确定的?/font>
因ؓ在这里创建的是一个空 string c,
长度?Qgؓ""(I??/font>
因此不存在str[0]?/font>
在函数内定义了一个一百万的数l?/font>
~译、连接没问题Q但执行时就会提C堆栈溢出?/font>
但定义全局变量却没有问题?/font>
谁能l我解释一下ؓ什么?
对于下面一D늨序:
#include<iostream>
using namespace std;
int main()
{
for(int i=0;i<10;i++)
{/*content*/}
for(int i=0;i<10;i++)
{/*content*/}
return 0;
}
在VC 6.0~译器中是“i?redefinition?/font>
而在Dev-C++~译器中是完全正的?/font>
我也一直搞不清楚i的作用域是什么?/font>
直到看过Primer后才知道有一U作用域叫做“语句作用域”?/font>
是说就上述问题的i只能在for语句中用?/font>
q就是说q是VC 6.0的一个Bug,真是q样吗?
题目说明要输入的字符串长度最多ؓ8?/font>
因此我就只申请了8个字W?/font>
l果当然是错了?/font>
其实字符数组需要一个存储单元来保存l束W“\0”,
用来说明一个字W串l束了?/font>
因此要申h字符串多一个存储单元的长度?/font>
C了!Q?/font>
从此不能犯同样错误!