您的位置:首页 > 编程语言 > C语言/C++

C++一些函数的用法

2012-11-30 14:05 295 查看
sprintf与printf的区别:

在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望。由于sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出。这也导致sprintf 比printf 有用得多。

  sprintf 是个变参函数,定义如下:

  int sprintf( char *buffer, const char *format [, argument] ... );

  除了前两个参数类型固定外,后面可以接任意多个参数。而它的精华,显然就在第二个参数:

  (1)格式化字符串上。

  printf 和sprintf 都使用格式化字符串来指定串的格式,在格式串内部使用一些以“%”开头的格式说明符(format specifications)来占据一个位置,在后边的变参列表中提供相应的变量,最终函数就会用相应位置的变量来替代那个说明符,产生一个调用者想要的字符串。

  格式化数字字符串

  sprintf 最常见的应用之一莫过于把整数打印到字符串中,所以,spritnf 在大多数场合可以替代itoa。

  如:

  //把整数123 打印成一个字符串保存在s 中。

  sprintf(s, "%d", 123); //产生"123"

  可以指定宽度,不足的左边补空格:

  sprintf(s, "%8d%8d", 123, 4567); //产生:" 123 4567"

  当然也可以左对齐:

  sprintf(s, "%-8d%8d", 123, 4567); //产生:"123 4567"

  也可以按照16 进制打印:

  sprintf(s, "%8x", 4567); //小写16 进制,宽度占8 个位置,右对齐

  sprintf(s, "%-8X", 4568); //大写16 进制,宽度占8 个位置,左对齐

  这样,一个整数的16 进制字符串就很容易得到,但我们在打印16 进制内容时,通常想要一种左边补0 的等宽格式,那该怎么做呢?很简单,在表示宽度的数字前面加个0 就可以了。

  sprintf(s, "%08X", 4567); //产生:"000011D7"

  上面以”%d”进行的10 进制打印同样也可以使用这种左边补0 的方式。

  这里要注意一个符号扩展的问题:比如,假如我们想打印短整数(short)-1 的内存16 进制表示形式,在Win32 平台上,一个short 型占2 个字节,所以我们自然希望用4 个16 进制数字来打印它:

  short si = -1;

  sprintf(s, "%04X", si);

  产生“FFFFFFFF”,怎么回事?因为spritnf 是个变参函数,除了前面两个参数之外,后面的参数都不是类型安全的,函数更没有办法仅仅通过一个“%X”就能得知当初函数调用前参数压栈时被压进来的到底是个4 字节的整数还是个2 字节的短整数,所以采取了统一4 字节的处理方式,导致参数压栈时做了符号扩展,扩展成了32 位的整数-1,打印时4 个位置不够了,就把32 位整数-1 的8 位16 进制都打印出来了。

  如果你想看si 的本来面目,那么就应该让编译器做0 扩展而不是符号扩展(扩展时二进制左边补0 而不是补符号位):

  sprintf(s, "%04X", (unsigned short)si);

  就可以了。或者:

  unsigned short si = -1;

  sprintf(s, "%04X", si);

  sprintf 和printf 还可以按8 进制打印整数字符串,使用”%o”。注意8 进制和16 进制都不会打

  印出负数,都是无符号的,实际上也就是变量的内部编码的直接的16 进制或8 进制表示。

  控制浮点数打印格式

  浮点数的打印和格式控制是sprintf 的又一大常用功能,浮点数使用格式符”%f”控制,默认保

  留小数点后6 位数字,比如:

  sprintf(s, "%f", 3.1415926); //产生"3.141593"

  但有时我们希望自己控制打印的宽度和小数位数,这时就应该使用:”%m.nf”格式,其中m 表

  示打印的宽度,n 表示小数点后的位数。比如:

  sprintf(s, "%10.3f", 3.1415626); //产生:" 3.142"

  sprintf(s, "%-10.3f", 3.1415626); //产生:"3.142 "

  sprintf(s, "%.3f", 3.1415626); //不指定总宽度,产生:"3.142"

  注意一个问题,你猜

  int i = 100;

  sprintf(s, "%.2f", i);

  会打出什么东东来?“100.00”?对吗?自己试试就知道了,同时也试试下面这个:

  sprintf(s, "%.2f", (double)i);

  第一个打出来的肯定不是正确结果,原因跟前面提到的一样,参数压栈时调用者并不知道跟i相对应的格式控制符是个”%f”。而函数执行时函数本身则并不知道当年被压入栈里的是个整数,于是可怜的保存整数i 的那4 个字节就被不由分说地强行作为浮点数格式来解释了,整个乱套了。不过,如果有人有兴趣使用手工编码一个浮点数,那么倒可以使用这种方法来检验一下你手工编排的结果是否正确。

20120914更新:

C++运算符优先级:

PrecedenceOperatorDescriptionExampleAssociativity
1()

[]

->

.

::

++

--
Grouping operator

Array access

Member access from a pointer

Member access from an object

Scoping operator

Post-increment

Post-decrement
(a + b) / 4;

array[4] = 2;

ptr->age = 34;

obj.age = 34;

Class::age = 2;

for( i = 0; i < 10; i++ ) ...

for( i = 10; i > 0; i-- ) ...
left to right
2!

~

++

--

-

+

*

&

(type)

sizeof
Logical negation

Bitwise complement

Pre-increment

Pre-decrement

Unary minus

Unary plus

Dereference

Address of

Cast to a given type

Return size in bytes
if( !done ) ...

flags = ~flags;

for( i = 0; i < 10; ++i ) ...

for( i = 10; i > 0; --i ) ...

int i = -1;

int i = +1;

data = *ptr;

address = &obj;

int i = (int) floatNum;

int size = sizeof(floatNum);
right to left
3->*

.*
Member pointer selector

Member pointer selector
ptr->*var = 24;

obj.*var = 24;
left to right
4*

/

%
Multiplication

Division

Modulus
int i = 2 * 4;

float f = 10 / 3;

int rem = 4 % 3;
left to right
5+

-
Addition

Subtraction
int i = 2 + 3;

int i = 5 - 1;
left to right
6<<

>>
Bitwise shift left

Bitwise shift right
int flags = 33 << 1;

int flags = 33 >> 1;
left to right
7<

<=

>

>=
Comparison less-than

Comparison less-than-or-equal-to

Comparison greater-than

Comparison geater-than-or-equal-to
if( i < 42 ) ...

if( i <= 42 ) ...

if( i > 42 ) ...

if( i >= 42 ) ...
left to right
8==

!=
Comparison equal-to

Comparison not-equal-to
if( i == 42 ) ...

if( i != 42 ) ...
left to right
9&Bitwise ANDflags = flags & 42;left to right
10^Bitwise exclusive ORflags = flags ^ 42;left to right
11|Bitwise inclusive (normal) ORflags = flags | 42;left to right
12&&Logical ANDif( conditionA && conditionB ) ...left to right
13||Logical ORif( conditionA || conditionB ) ...left to right
14? :Ternary conditional (if-then-else)int i = (a > b) ? a : b;right to left
15=

+=

-=

*=

/=

%=

&=

^=

|=

<<=

>>=
Assignment operator

Increment and assign

Decrement and assign

Multiply and assign

Divide and assign

Modulo and assign

Bitwise AND and assign

Bitwise exclusive OR and assign

Bitwise inclusive (normal) OR and assign

Bitwise shift left and assign

Bitwise shift right and assign
int a = b;

a += 3;

b -= 4;

a *= 5;

a /= 2;

a %= 3;

flags &= new_flags;

flags ^= new_flags;

flags |= new_flags;

flags <<= 2;

flags >>= 2;
right to left
16,Sequential evaluation operatorfor( i = 0, j = 0; i < 10; i++, j++ ) ...left to right
20120917更新:

C++中的list用法

#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>

using namespace std;

//创建一个list容器的实例LISTINT
typedef list<int> LISTINT;

//创建一个list容器的实例LISTCHAR
typedef list<int> LISTCHAR;

void main(void)
{
//--------------------------
//用list容器处理整型数据
//--------------------------
//用LISTINT创建一个名为listOne的list对象
LISTINT listOne;
//声明i为迭代器
LISTINT::iterator i;

//从前面向listOne容器中添加数据
listOne.push_front (2);
listOne.push_front (1);

//从后面向listOne容器中添加数据
listOne.push_back (3);
listOne.push_back (4);

//从前向后显示listOne中的数据
cout<<"listOne.begin()--- listOne.end():"<<endl;
for (i = listOne.begin(); i != listOne.end(); ++i)
cout << *i << " ";
cout << endl;

//从后向后显示listOne中的数据
LISTINT::reverse_iterator ir;
cout<<"listOne.rbegin()---listOne.rend():"<<endl;
for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {
cout << *ir << " ";
}
cout << endl;

//使用STL的accumulate(累加)算法
int result = accumulate(listOne.begin(), listOne.end(),0);
cout<<"Sum="<<result<<endl;
cout<<"------------------"<<endl;

//--------------------------
//用list容器处理字符型数据
//--------------------------

//用LISTCHAR创建一个名为listOne的list对象
LISTCHAR listTwo;
//声明i为迭代器
LISTCHAR::iterator j;

//从前面向listTwo容器中添加数据
listTwo.push_front ('A');
listTwo.push_front ('B');

//从后面向listTwo容器中添加数据
listTwo.push_back ('x');
listTwo.push_back ('y');

//从前向后显示listTwo中的数据
cout<<"listTwo.begin()---listTwo.end():"<<endl;
for (j = listTwo.begin(); j != listTwo.end(); ++j)
cout << char(*j) << " ";
cout << endl;

//使用STL的max_element算法求listTwo中的最大元素并显示
j=max_element(listTwo.begin(),listTwo.end());
cout << "The maximum element in listTwo is: "<<char(*j)<<endl;
}

#include <iostream>
#include <list>

using namespace std;
typedef list<int> INTLIST;

//从前向后显示list队列的全部元素
void put_list(INTLIST list, char *name)
{
INTLIST::iterator plist;

cout << "The contents of " << name << " : ";
for(plist = list.begin(); plist != list.end(); plist++)
cout << *plist << " ";
cout<<endl;
}

//测试list容器的功能
void main(void)
{
//list1对象初始为空
INTLIST list1;
//list2对象最初有10个值为6的元素
INTLIST list2(10,6);
//list3对象最初有3个值为6的元素
INTLIST list3(list2.begin(),--list2.end());

//声明一个名为i的双向迭代器
INTLIST::iterator i;

//从前向后显示各list对象的元素
put_list(list1,"list1");
put_list(list2,"list2");
put_list(list3,"list3");

//从list1序列后面添加两个元素
list1.push_back(2);
list1.push_back(4);
cout<<"list1.push_back(2) and list1.push_back(4):"<<endl;
put_list(list1,"list1");

//从list1序列前面添加两个元素
list1.push_front(5);
list1.push_front(7);
cout<<"list1.push_front(5) and list1.push_front(7):"<<endl;
put_list(list1,"list1");

//在list1序列中间插入数据
list1.insert(++list1.begin(),3,9);
cout<<"list1.insert(list1.begin()+1,3,9):"<<endl;
put_list(list1,"list1");

//测试引用类函数
cout<<"list1.front()="<<list1.front()<<endl;
cout<<"list1.back()="<<list1.back()<<endl;

//从list1序列的前后各移去一个元素
list1.pop_front();
list1.pop_back();
cout<<"list1.pop_front() and list1.pop_back():"<<endl;
put_list(list1,"list1");

//清除list1中的第2个元素
list1.erase(++list1.begin());
cout<<"list1.erase(++list1.begin()):"<<endl;
put_list(list1,"list1");

//对list2赋值并显示
list2.assign(8,1);
cout<<"list2.assign(8,1):"<<endl;
put_list(list2,"list2");

//显示序列的状态信息
cout<<"list1.max_size(): "<<list1.max_size()<<endl;
cout<<"list1.size(): "<<list1.size()<<endl;
cout<<"list1.empty(): "<<list1.empty()<<endl;

//list序列容器的运算
put_list(list1,"list1");
put_list(list3,"list3");
cout<<"list1>list3: "<<(list1>list3)<<endl;
cout<<"list1<list3: "<<(list1<list3)<<endl;

//对list1容器排序
list1.sort();
put_list(list1,"list1");

//结合处理
list1.splice(++list1.begin(), list3);
put_list(list1,"list1");
put_list(list3,"list3");
}


20120920更新:

C++中定义全局变量要注意的地方

在C++中定义全局变量是应该尽量在。cpp文件中定义,而不要在。h 文件中 定义,定义好了之后,可以在。h文件中利用 extern关键字进行 声明。如果在。h文件中定义的话,多层包含可能会引起重复定义的错误。下面是一个示例

  在base.cpp中定义全局变量

  base.cpp

  int g_MaxTime;

  int g_MinTime;

  int g_MaxCount;

  int g_MinCount;

  base.h

  extern int g_MaxTime;

  extern int g_MinTime;

  extern int g_MaxCount;

  extern int g_MinCount;

  然后其他文件要使用这些变量的时候

  只要#include "base.h"就可以了,而且不会引起重复定义的错误

C++语法问题:

printf(count%8?"%2d:%d,%d,%d":"%2d:%d,%d,%d\n",++count,i,j,m);

如果count不是8的倍数,就按照?"%2d:%d,%d,%d"的格式输出,是8的倍数则按照"%2d:%d,%d,%d\n"的格式输出.
是每输出8个数换行

2012年11月14日更新:

计算两向量的旋转角(转)

向量的点乘和叉乘都是很有用的数学工具,不过他们也有局限性。关键是向量点乘可以得到两个向量之间的夹角, 而不是旋转角,这个角度是没有方向的,范围是[0-pi], 而这往往不是我们想要的, 实际问题中我们常常要计算从向量p1沿逆时针方向转到与向量p2方向一致的确切角度,我把这个角度定义为旋转角。 旋转角的计算既需要夹角,还需要两个向量的叉乘, 以确定p1和p2的角度方向关系。

关于叉乘符号与向量的角度方向关系,请参考《算法导论》,我只给出结论:

p1 * p2 = x1y2 - x2 y1 = -p2 * p1

If p1 * p2 is positive, then p1 is clockwise from p2 with respect to the origin (0, 0); if this cross product is negative, then p1 is counterclockwise from p2.

另外考虑的是共线(collinear )的问题, arcsine很难处理这个问题, 不过arecosine却能够明确的区分0和pi,因此作为特殊情况提前得出结论。

注:因为主要是openGL要用, 所以返回的是角度值

/************************************************************************/
/* author  : Navy@hust
* file    : angle.c
* date     : 5/12/2007
* desc     : vector rotate angle calculation
/************************************************************************/
#i nclude <stdio.h>
#i nclude <math.h>
double getRotateAngle(double x1, double y1, double x2, double y2);
int main(int argc, char **argv)
{
double x1, x2, y1, y2;
double dist, dot, degree, angle;
freopen("angle.in", "r", stdin);

while(scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2) == 4) {
printf("the rotate angle from p1 to p2 is %.3lf\n",
getRotateAngle(x1, y1, x2, y2));
}
}
/*
* 两个向量之间的旋转角
* 首先明确几个数学概念:
* 1. 极轴沿逆时针转动的方向是正方向
* 2. 两个向量之间的夹角theta, 是指(A^B)/(|A|*|B|) = cos(theta),0<=theta<=180 度, 而且没有方向之分
* 3. 两个向量的旋转角,是指从向量p1开始,逆时针旋转,转到向量p2时,所转过的角度, 范围是 0 ~ 360度
* 计算向量p1到p2的旋转角,算法如下:
* 首先通过点乘和arccosine的得到两个向量之间的夹角
* 然后判断通过差乘来判断两个向量之间的位置关系
* 如果p2在p1的顺时针方向, 返回arccose的角度值, 范围0 ~ 180.0(根据右手定理,可以构成正的面积)
* 否则返回 360.0 - arecose的值, 返回180到360(根据右手定理,面积为负)
*/
double getRotateAngle(double x1, double y1, double x2, double y2)
{
const double epsilon = 1.0e-6;
const double nyPI = acos(-1.0);
double dist, dot, degree, angle;

// normalize
dist = sqrt( x1 * x1 + y1 * y1 );
x1 /= dist;
y1 /= dist;
dist = sqrt( x2 * x2 + y2 * y2 );
x2 /= dist;
y2 /= dist;
// dot product
dot = x1 * x2 + y1 * y2;
if ( fabs(dot-1.0) <= epsilon )
angle = 0.0;
else if ( fabs(dot+1.0) <= epsilon )
angle = nyPI;
else {
double cross;

angle = acos(dot);
//cross product
cross = x1 * y2 - x2 * y1;
// vector p2 is clockwise from vector p1
// with respect to the origin (0.0)
if (cross < 0 ) {
angle = 2 * nyPI - angle;
}
}
degree = angle *  180.0 / nyPI;
return degree;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: