前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >题目1003:A+B ---c_str(),atoi()函数的使用;remove , erase函数的使用

题目1003:A+B ---c_str(),atoi()函数的使用;remove , erase函数的使用

作者头像
猿人谷
发布2018-01-17 15:16:27
7450
发布2018-01-17 15:16:27
举报
文章被收录于专栏:猿人谷猿人谷
代码语言:javascript
复制
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int sw(char *a){
 4         int i=0,c=0;
 5         while(a[i]){
 6                 if(a[i]>='0'&&a[i]<='9')
 7                         c=c*10+a[i]-'0';
 8                     i++;
 9         }
10         if(a[0]=='-')
11                 c=-c;
12         return c;
13 }
14 int main(){
15         char a[99],b[99];
16         int a1,b1,c[99],i=0;
17         while(scanf("%s %s",a,b)!=EOF)
18         {
19                 a1=sw(a);
20                 b1=sw(b);
21         
22         c[i]=a1+b1;
23         i++;
24         }
25         for(int j=0;j<i;j++)
26                 printf("%d\n",c[j]);
27         return 0;
28 }
代码语言:javascript
复制
 1 #include<iostream>  
 2 #include<string>  
 3 #include<algorithm>  
 4 using namespace std;  
 5 int main(){  
 6     //freopen("a.txt","r",stdin);  
 7     char inA[20]={0};  
 8     char inB[20]={0};  
 9     while(cin>>inA>>inB)
10     {  
11         string strA(inA);  
12         string strB(inB);  
13         strA.erase(remove(strA.begin(),strA.end(),','),strA.end()); 
14         strB.erase(remove(strB.begin(),strB.end(),','),strB.end());  
15         int a = atoi(strA.c_str());  //c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.
16         int b = atoi(strB.c_str());  //atoi()会将字符串转换为整型数
17         cout<<a+b<<endl;  
18         }  
19     //  getchar();  
20 
21      
22    return 0;
23 }

 关于如下两行代码的使用很让人疑惑:

image.png
image.png

接下来就举例子来说明一下:

代码语言:javascript
复制
 1 #include <iostream>
 2 
 3 #include <list>
 4 
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 int main()
10 
11 {
12 
13     list<int> coll;
14 
15        //insert elements from 6 to 1 and 1 to 6
16 
17     for (int i=1; i<=6; ++i) {
18 
19          coll.push_front(i);
20 
21          coll.push_back(i);
22 
23     }
24 
25     //print all elements of the collection
26 
27     cout << "pre:  ";
28 
29     copy (coll.begin(), coll.end(), ostream_iterator<int> (cout," "));     
30 
31     cout << endl;
32 
33     //remove all elements with value 3
34 
35     remove (coll.begin() , coll.end(),  3);   
36 
37     //print all elements of the collection
38 
39     cout << "post: ";
40 
41     copy (coll.begin(), coll.end(), ostream_iterator<int> (cout," "));     
42 
43     cout << endl;
44 
45 }

执行remove动作后,值为3的节点还在,只是里面的值被后续节点的值覆盖了,整个容器的长度没有改变。

向左转|向右转

这样调整一下,就达到目的了。

代码语言:js
复制
list<int>::iterator end = remove (coll.begin(), coll.end(),  3); 
coll.erase (end, coll.end());

c_str()的用法:

代码语言:javascript
复制
语法: 
const char *c_str();
c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针 
比如:最好不要这样: 
char* c; 
string s="1234"; 
c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理

应该这样用: 
char c[20]; 
string s="1234"; 
strcpy(c,s.c_str()); 
这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作

再举个例子
c_str() 以 char* 形式传回 string 内含字符串
如果一个函数要求char*参数,可以使用c_str()方法: 
string s = "Hello World!";
printf("%s", s.c_str()); //输出 "Hello World!"

atoi函数的使用:

    原型:int  atoi (const  char  *nptr)

    用法:#include  <stdlib.h>

    功能:将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回。

    说明:atoi()函数返回转换后的整型数。

    可参考:http://blog.csdn.net/youbang321/article/details/7888138

举例:

代码语言:js
复制
#include <stdio.h> 
#include <stdlib.h> 
 
int main()  
{  
 char a[] = "-100";  
 char b[] = "456";  
 int c = 0;  
 
    c = atoi(a) + atoi(b);  
 
    printf("c = %d\n",c);  
}  

    结果:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-08-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档