c++一些问题总结

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">这里总结一些c++常遇到的问题</span></span>

不同类型之间的转换。

<span style="font-size:18px;">//1 string --> const char*
    std::string s_1 = "lsw";
    const char *cs_1 = s_1.c_str();
    printf("const char * cs is %s \n", cs_1);
    
    //2 const char* --> string
    const char *cs_2 = "lsw";
    std::string s_2(cs_2);
    printf("std::string s_2 is %s\n", s_2.c_str());
    
    //3 string --> char*
    std::string s_3 = "lsw";
    char *cs_3;
    auto len = s_3.length();
    cs_3 = new char[len + 1];
    char *res_3 = strcpy(cs_3, s_3.c_str());
    printf("string to char* === %s", res_3);
    
    //4 char* --> string
    char *cs_4 = "lsw"; //c++ 11标准中这里有警告,不推荐这么用
    std::string s_4(cs_4);
    
    //5 const char* --> char *
    const char* cs_5 = "lsw";
    char *cs_6 = new char[100];//足够大
    char *res_5 = strcpy(cs_6, cs_5);    </span><p class="p1"><span style="font-size:18px;"><span class="s1">    printf</span><span class="s2">(</span>"cs_6 = %s \n"<span class="s2">, cs_6);</span></span></p><p class="p1"><span class="s2"><span style="font-size:18px;">
</span></span></p>

string, const char* ---> int, double, long

<span style="font-size:18px;">double	atof(const char *);
int	atoi(const char *);
long	atol(const char *);</span>


int --- > string

<span style="font-size:18px;">    char buff[100];
    sprintf(buff, "%d", 990);
    std::string sb = buff;</span>


已知strcpy的函数原型:char *strcpy(char *strDest, const char *strSrc)其中strDest 是目的字符串,strSrc 是源字符串。不调用C++/C 的字符串库函数,请编写函数 strcpy

/**
 已知strcpy函数的原型是
 char *strcpy(char *strDest, const char *strSrc);
 其中strDest是目的字符串,strSrc是源字符串。
 (1)不调用C++/C的字符串库函数,请编写函数 strcpy
 (2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?
 答:为了 实现链式表达式。
 例如 int length = strlen( strcpy( strDest, “hello world”) );
 */
char *myStrcpy(char *str1, const char *str2) {
    assert(str1 != nullptr && (str2 != nullptr));
    char *res = str1;
    while ((*str1++ = *str2++) != '\0') {
        continue;
    }
    
    return res;
}

int myStrLen(const char* str) {
    assert(str != nullptr);
    auto len = 0;
    while (*str++ != '\0') {
        ++ len;
    }
    
    return len;
}


其他的一些知识

1、sizeof 和 strlen

char a[] = "12";
//这里sizeof输出 3 是a的位数包括 '\0'
cout << sizeof(a) << endl; 

char *p = a;
//输出 8,是指针p的字节数
cout << sizeof(p) << endl;

char *str = "12";
//输出 2,不包含'\0'
cout << strlen(str) << endl;

2、宏定义
#define Min(a, b) ((a)>=(b)?(b):(a))

3、string定义

.h
//
//  MyString.h
//  TestCPP
//
//  Created by lsw on 14-12-24.
//  Copyright (c) 2014年 lsw. All rights reserved.
//

#ifndef __TestCPP__MyString__
#define __TestCPP__MyString__

#include <stdio.h>
class MyString {
public:
    MyString(const char *str = NULL); // 普通构造函数
    MyString(const MyString &other); // 拷贝构造函数
    ~MyString(void); // 析构函数
    MyString & operator =(const MyString &other); // 赋值函数
private:
    char *m_data; // 用于保存字符串
    
private:
    int myStrlen(const char* str);
};

#endif /* defined(__TestCPP__MyString__) */



.cpp
//
//  MyString.cpp
//  TestCPP
//
//  Created by lsw on 14-12-24.
//  Copyright (c) 2014年 lsw. All rights reserved.
//

#include "MyString.h"
#include <iostream>
#include <assert.h>

MyString::MyString(const char* str) {
    if (str == nullptr) {
        m_data = new char[1];
        m_data[0] = '\0';
    } else {
        auto len = myStrlen(str);
        m_data = new char[len + 1];
        assert(m_data != nullptr);
        strcpy(m_data, str);
    }
}

MyString::MyString(const MyString &other) {
    auto len = strlen(other.m_data);
    m_data = new char[len + 1];
    assert(m_data != nullptr);
    strcpy(m_data, other.m_data);
}

MyString::~MyString() {
    delete [] m_data;
}

MyString & MyString::operator=(const MyString &other) {
    if (this == &other) {
        return *this;
    }
    
    delete [] m_data;
    auto len = strlen(other.m_data);
    m_data = new char[len + 1];
    assert(m_data != nullptr);
    strcpy(m_data, other.m_data);
    return *this;
}

int MyString::myStrlen(const char *str) {
    assert(str != nullptr);
    
    int len = 0;
    while (*str++ != '\0') {
        ++len;
    }
    
    return len;
}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值