string & cstring

来源:互联网 发布:索尼c6802怎么清除数据 编辑:程序博客网 时间:2024/06/06 03:46
英文题面:
Implement the two functions which proceed to exchange between string and cstring.
 

(1) std::string change1(char* st);

    Convert a string of the cstring type to one belonging to the string type.

(2) void change2(std::string st1, char* st2)

    Convert a string of the string type to one belonging to the cstring type.

   

You should only submit the declaration of the two function.

 

 

中文题面:

请实现以下两个函数:

(1)std::string change1(char* st);

    把cstring类的字符串转成string类的字符串

(2)void change2(std::string st1, char* st2)

    把string类的字符串转成cstring类的字符串

exchange.h

#include <string>std::string change1(char* st) {    std::string tmp = "";    for (int i = 0; st[i] != '\0'; i++)        tmp += st[i];    return tmp;} void change2(std::string st1, char* st2) {    for (int i = 0; i < st1.length(); i++)        st2[i] = st1[i];}
main.cpp

#include <cstdio>#include <iostream>#include <cstring>#include <string>#include "exchange.h"std::string st1;char st2[100];int main() {    std::cin >> st1;    change2(st1, st2);    puts(st2);    scanf("%s", st2);    std::cout << change1(st2) << std::endl;    return 0;}
cstring和string的区别感觉不是很大,string是在STL中实现,加入c++标准库中,cstring是主要应用在MFC和ATL中。


 

0 0
原创粉丝点击