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

简介:
复制代码
 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 }
复制代码

 

复制代码
 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 }
复制代码

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

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

复制代码
 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的节点还在,只是里面的值被后续节点的值覆盖了,整个容器的长度没有改变。

向左转 | 向右转

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

list<int>::iterator end = remove (coll.begin(), coll.end(),  3); 

coll.erase (end, coll.end());

 

 

 

c_str()的用法:

复制代码
语法: 
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

举例:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. int main()  
  5. {  
  6.     char a[] = "-100";  
  7.     char b[] = "456";  
  8.     int c = 0;  
  9.       
  10.     c = atoi(a) + atoi(b);  
  11.       
  12.     printf("c = %d\n",c);  
  13. }  

    结果:




本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/3263568.html,如需转载请自行联系原作者

目录
相关文章
|
6天前
|
容器
常用库函数的用法——memset() / swap() / reverse() / unique()函数的用法
常用库函数的用法——memset() / swap() / reverse() / unique()函数的用法
10 0
|
4月前
从接口获取获取到数组arr=[‘1‘,‘a‘,‘2‘,‘b‘,‘3‘,‘c‘]转换成{number:‘123’,char:‘abc’}
从接口获取获取到数组arr=[‘1‘,‘a‘,‘2‘,‘b‘,‘3‘,‘c‘]转换成{number:‘123’,char:‘abc’}
|
9月前
|
算法 IDE 开发工具
【Python语法】类型提示(self, nums: List[int]) -> List[int],报错NameError: name ‘List‘ is not defined解决
【Python语法】类型提示(self, nums: List[int]) -> List[int],报错NameError: name ‘List‘ is not defined解决
|
9月前
strstr(str1,str2) 函数与sscanf()函数功能详解
strstr(str1,str2) 函数与sscanf()函数功能详解
Len()、Lenw()与Lenb()函数间的区别
Len()、Lenw()与Lenb()函数间的区别
|
11月前
|
C++
C++ 递归和非递归实现字符串反转 char *reverse(char *s)
C++ 递归和非递归实现字符串反转 char *reverse(char *s)
80 0
【C/C++ strlen(str)和str.length()和str.size()的区别】
strlenQ(str)和str.length()和str.size()都可以求字符串长度,返回字符串中字符的长度,不包括0'。其中str.length()和str.size()是同义词,返回同样的值。
【C/C++ strlen(str)和str.length()和str.size()的区别】
|
SQL
LEN() 函数
LEN() 函数
71 0
编写一个函数reverse_string(char*string),实现将参数字符串中的倒叙 如 char arr[]=“abcdef“变为“fedcba“
编写一个函数reverse_string(char*string),实现将参数字符串中的倒叙 如 char arr[]=“abcdef“变为“fedcba“
142 0
编写一个函数reverse_string(char*string),实现将参数字符串中的倒叙 如 char arr[]=“abcdef“变为“fedcba“
|
机器学习/深度学习 C语言
【C 语言】数组 ( 指针退化验证 | 计算数组大小 | #define LENGTH(array) (sizeof(array) / sizeof(*array)) )
【C 语言】数组 ( 指针退化验证 | 计算数组大小 | #define LENGTH(array) (sizeof(array) / sizeof(*array)) )
172 0
【C 语言】数组 ( 指针退化验证 | 计算数组大小 | #define LENGTH(array) (sizeof(array) / sizeof(*array)) )