一些容易被大家忽视的C语言函数和一些C++函数的基本应用

来源:互联网 发布:电脑桌面日程表软件 编辑:程序博客网 时间:2024/05/17 17:16

1.itoa函数

  那么这个函数呢其实就是起到将整型数值转换为字符串例如将1234转换成为字符串输出,可以这么使用itoa(number,string,10)这里面有三个参数,第一个参数代表的整形数据,第二个参数代表的是字符串名,第三的代表的是你读入的是多少进制的数字,比如这里是个10那就是以10进制。

2.memcpy函数

  这个函数的话主要是复制用可以复制整形结构体也可以复制字符串之类的,memcpy(d,s,strlen(s)+1)这个复制不会遇到'\0'就停下来,而是会继续复制直到你让他提供复制到哪里为止就是第三个参数决定的。

3.substr函数

  这个函数的话其实就是也差不多是个字符串复制函数举个例子吧,string s="12345abchdj" string a=s.substr(0,5),则a为“12345”,从0开始复制一直到n-1为止。此处n代表第二个参数

4.find函数

  find函数可以用来寻找子串也可以用来寻找每个字符,s.find(s1),其中返回的是所在下标。找到了才会返回下标。否则返回的会无穷大。find在string当中的应用

#include <iostream>#include <algorithm>#include <string>using namespace std;int main(){    string s = "helllo";    if (s.find("l") == string::npos)        cout << "NO" << endl;    else        cout << "YES" << endl;    if (s.find("o") == string::npos)        cout << "NO" << endl;    else        cout << "YES" << endl;        return 0;}
find在set中的应用
#include <iostream>#include <algorithm>#include <string>#include <set>using namespace std;int main(){    set<string> m;    m.insert("Hello");    m.insert("hello");    if (find(m.begin(), m.end(), "Hello") == m.end())        cout << "no" << endl;    else        cout << "yes" << endl;    if (find(m.begin(), m.end(), "hello") == m.end())        cout << "no" << endl;    else        cout << "yes" << endl;     return 0;}
vector也是差不多的。

5.map容器

  例如map<long long,int> a中其实这个和数组类似的比如a[n]=1;这样表示的话,n的数据类型是long long,而1那个值是int型,map的删除的话就是 比如a.erase(i)其中i代表的是下标。

for (map<string, string>::iterator i=a.begin(); i!=a.end(); i++)      {          if (a->first == "CZX")//first代表下标,a->second代表键值          {          }      }  //遍历应用
6.vector容器

  这个也是容器比如vector<int> a(n,0)意思也差不多和数组类似,简单介绍下在定义的时候a(n,0)表示a这个容器有n个元素,并且初值都是0;

7.set容器

  这个也是需要注意的,这里讲一下他的插入吧这个插入是不加重复的元素的会自动按照升序排序的。

8.next_permutation函数

  这个函数是个全排列函数可以快速的输出全排列的问题下面是它的基本应用

#include <iostream>#include <algorithm>using namespace std;int main(){    int num[3]={1,2,3};    do    {        cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;    }while(next_permutation(num,num+3));    return 0;}



0 0