程序设计竞赛——C++常用库函数

本文详细介绍了C++编程中常用的库函数,包括memcpy、strncpy、位运算和类型转换等,并通过示例代码展示了它们的使用方法。此外,还提及了其他如字符串处理、数学和浮点数转换的函数。
摘要由CSDN通过智能技术生成

首先就是memcpy

表头文件: #include <string.h>

定义函数: void *memcpy(void *dest, const void*src, size_t n)

函数说明: memcpy()用来拷贝src所指的内存内容前n个字节到dest所指的内存地址上。与strcpy()不同的是,memcpy()会完整的复制n个字节,不会因为遇到字符串结束'\0'而结束

返回值:  返回指向dest的指针

附加说明: 指针src和dest所指的内存区域不可重叠

例如:

你需要复制串str=“wangyucao1989”中的“yucao”,那么可以这么写:

memcpy(newstr,str+4,5);

 

除了memcpy之外,string还提供了strncpy函数:

函数名称: strncpy

函数原型: char *strncpy(char *dest, const char*src,int count)

函数功能: 将字符串src中的count个字符拷贝到字符串dest中去

函数返回: 指向dest的指针

参数说明: dest-目的字符串,src-源字符串,count-拷贝的字符个数

所属文档: <string.h>

 

还是上面的例子,这个程序可以这样写:

#include<stdio.h>

#include<string.h>

int main()

{

    char str[] ="wangyucao1989";

 

    charnewstr[6];

   //memcpy(newstr,str+4,5);

   strncpy(newstr,str+4,5);

    newstr[5] ='\0';

   printf("%s\n",newstr);

    return 0;

}

 

========================================================

位运算:

运算方法有六种:

 

& 与运算

| 或运算

^ 异或运算

~ 非运算(求补)

>> 右移运算

<< 左移运算

 

 

运用这些基本的运算,我们可以解决acm所需的各种运算,给Bit赋1,赋0,给他的值取反,还有好多段操作。如下:

 

 

功能             |      示例           | 位运算

-----------------+---------------------+--------------------

去掉最后一位    | (101101->10110)   | x>> 1

在最后加一个0   | (101101->1011010) | x < < 1

在最后加一个1   | (101101->1011011) | x < < 1+1

把最后一位变成1 | (101100->101101)  | x | 1

把最后一位变成0 | (101101->101100)  | x | 1-1

最后一位取反    | (101101->101100)  | x ^ 1

把右数第k位变成1 |(101001->101101,k=3) | x | (1 < < (k-1))

把右数第k位变成0 | (101101->101001,k=3)| x & ~ (1 < < (k-1))

右数第k位取反    | (101001->101101,k=3) | x ^ (1 << (k-1))

取末三位        | (1101101->101)       | x& 7

取末k位          | (1101101->1101,k=5)  | x & ((1 < < k)-1)

取右数第k位      | (1101101->1,k=4)     | x >> (k-1) & 1

把末k位变成1     | (101001->101111,k=4) | x | (1 << k-1)

末k位取反        | (101001->100110,k=4) | x ^ (1 << k-1)

把右边连续的1变成0 |(100101111->100100000) | x & (x+1)

把右起第一个0变成1 |(100101111->100111111) | x | (x+1)

把右边连续的0变成1 |(11011000->11011111)   | x | (x-1)

取右边连续的1     | (100101111->1111)      | (x ^ (x+1)) >> 1

去掉右起第一个1的左边 |(100101000->1000)   | x & (x ^(x-1))

判断奇数 (x&1)==1

判断偶数 (x&1)==0

取右边第一个1所在位置 x&-x

 

================================================================

类型转换:

函数名: abs 功  能: 求整数的绝对值

用  法: int abs(int i);

程序例:

#include <stdio.h>

#include <math.h>

 

int main(void)

{

  int number =-1234;

 

 printf("number: %d  absolutevalue: %d\n", number, abs(number));

  return 0;

}

函数名: atof

功  能: 把字符串转换成浮点数

用  法: double atof(const char *nptr);

程序例:

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

   float f;

   char *str ="12345.67";

 

   f =atof(str);

  printf("string = %s float = %f\n", str, f);

   return 0;

}

 

 

 

函数名: atoi

功  能: 把字符串转换成长整型数

用  法: int atoi(const char *nptr);

程序例:

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

   int n;

   char *str ="12345.67";

 

   n =atoi(str);

  printf("string = %s integer = %d\n", str, n);

   return 0;

}

 

 

 

函数名: atol

功  能: 把字符串转换成长整型数

用  法: long atol(const char *nptr);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

   long l;

   char *str ="98765432";

 

   l &#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值