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

向左转 | 向右转

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

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

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. }  

    结果:

你可能感兴趣的:(remove)