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

随机推荐

  1. Mybatis 插入与批量插入以及多参数批量删除

    实体类: import java.io.Serializable; public class AttachmentTable implements Serializable { private sta ...

  2. Repeater上下排序按钮

    aspx代码 <table cellspacing="0" cellpadding="0" width="100%" align=&q ...

  3. UINavigationController的使用

    1.UINavigationController使用流程 UINavigationController为导航控制器,在iOS里经常用到. 我们看看它的如何使用: 下面的图显示了导航控制器的流程.最左侧 ...

  4. HDU 5340 Three Palindromes (Manacher)

    题意: 判断是否能将字符串S分成三段非空回文串. 思路: 先预处理出前缀回文串和后缀回文串的位置,将位置分别装入两个集合中,O(n). 针对每个前缀回文串的终点位置,挑出不相交的后缀回文串,对中间那段 ...

  5. Spring3.1中使用profile配置开发测试线上环境

    如果在开发时进行一些数据库测试,希望链接到一个测试的数据库,以避免对开发数据库的影响. 开发时的某些配置比如log4j日志的级别,和生产环境又有所区别. 各种此类的需求,让我希望有一个简单的切换开发环 ...

  6. *ecshop安装模板

    1. 安装模板 例: 新建abc.com文件 复制default下的style.css 修改两个http://www.ecshop.com/ 为 http://www.hua.com/ 复制defau ...

  7. Oracle Exadata体系笔记

    Exadata一开始是以一个存储系统形式诞生的,叫做SAGE(Storage Appliance for Grid Environ ments,网格环境存储设备)   Exadata原本设计用来解决超 ...

  8. Excel学习笔记杂荟

    Excel学习 一.工具->选项 可以对整个excel里面的东西进行编辑,里面有隐藏行号,下拉档等选项,有文档作者信息. 隐藏网格等 二.单元格内容比较大可以右击单元格->设置单元格格式- ...

  9. MatrixTurn源码阅读

    在看cacheAsBitmap 相关资料时,找到bit101的一篇文章,http://www.bytearray.org/?p=290 全文如下: One of the feature I would ...

  10. android view生命周期

    onFinishInflate() 当View中所有的子控件均被映射成xml后触发 onMeasure( int ,  int ) 确定所有子元素的大小 onLayout( boolean ,  in ...