码迷,mamicode.com
首页 > 编程语言 > 详细

关于C中字符数组,字符指针以及C++中string类型的两两转换及排序

时间:2017-03-10 22:27:34      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:应用   类型   ror   rar   ios   name   bsp   string类   erro   

 

 

// practise.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
    /* 字符串转字符数组,使用strncpy_s(),比strncpy()安全*/
    string str = "abc xyz";
    char strArray[11] = {0};
    strncpy_s(strArray, str.c_str(), str.length());
    printf("strArray: %s\n", strArray);
    cout << strArray << endl;

    /* 字符串转字符指针,直接调用c_str()*/
    const char* strPointer = "";
    strPointer = str.c_str();
    printf("strPointer: %s\n", strPointer);
    cout << strPointer << endl;

    /* 字符指针转成字符串,直接赋值*/
    char* strPointer1 = "bat hzx";
    string str1 = strPointer1;
    printf("str1: %s\n", str1.c_str());

    /* 字符数组转成字符串,直接赋值*/
    char strArray1[] = "hzx bat";
    string str2 = strArray1;
    printf("str2: %s\n", str2.c_str());

    /* 字符指针转成字符数组,strncpy_s()*/
    char* strPointer2 = "cheng du";
    //int strLen = strlen(strPointer2);
    //char strArray2[strLen] = {0};//error C2057: 应输入常量表达式
    char strArray2[11] = {0};
    strncpy_s(strArray2, strPointer2, strlen(strPointer2));
    printf("strArray2: %s\n", strArray2);

    /* 字符数组转成字符指针,直接赋值*/
    char strArray3[16] = "Beijing China";
    char* strPointer3;
    strPointer3 = strArray3;
    printf("strPointer3: %s\n", strPointer3);

    /* 字符数组如何比较大小,使用strcmp()函数*/
    char strArray4[8] = "abc";
    char strArray5[8] = "abx";
    char strArray6[8] = "abc";
    printf("strArray4: %d  strArray5: %d  strArray6: %d\n", strArray4, strArray5, strArray6);
    printf("is big: %d\n", strcmp(strArray4, strArray5));
    printf("is big: %d\n", strcmp(strArray4, strArray6));
    printf("is big: %d\n", strcmp(strArray5, strArray6));

    /* 字符指针如何比较大小,使用strcmp()函数*/
    char* strArray7 = "abc";
    char* strArray8 = "aby";
    char* strArray9 = "abc";
    printf("strArray7: %d  strArray8: %d  strArray9: %d\n", strArray7, strArray8, strArray9);
    printf("is big: %d\n", strcmp(strArray7, strArray8));
    printf("is big: %d\n", strcmp(strArray7, strArray9));
    printf("is big: %d\n", strcmp(strArray8, strArray9));

    /* 字符指针和字符数组如何比较大小,使用strcmp()函数*/
    char* strArray10 = "abc";
    char strArray11[5] = "abz";
    char* strArray12 = "abc";
    printf("strArray10: %d  strArray11: %d  strArray12: %d\n", strArray10, strArray11, strArray12);
    printf("is big: %d\n", strcmp(strArray10, strArray11));
    printf("is big: %d\n", strcmp(strArray10, strArray12));
    printf("is big: %d\n", strcmp(strArray11, strArray12));

    /* 字符串如何比较大小,使用strcmp()+c_str()组合或者直接compare()*/
    string str13 = "abc";
    string str14 = "abx";
    string str15 = "abc";
    printf("str13: %d  str14: %d  str15: %d\n", str13, str14, str15);
    printf("is big: %d\n", strcmp(str13.c_str(), str14.c_str()));
    printf("is big: %d\n", strcmp(str13.c_str(), str15.c_str()));
    printf("is big: %d\n", strcmp(str14.c_str(), str15.c_str()));
    cout << str13.compare(str14) << endl;
    cout << str13.compare(str15) << endl;
    cout << str14.compare(str15) << endl;
    return 0;
}

 

关于C中字符数组,字符指针以及C++中string类型的两两转换及排序

标签:应用   类型   ror   rar   ios   name   bsp   string类   erro   

原文地址:http://www.cnblogs.com/liuzc/p/6533110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!