CC++一些库函数的实现

mac2022-07-05  29

1. 写出String的具体实现

类的定义:

#include <iostream> #include <cstring> using namespace std; class String { friend ostream & operator<<(ostream &, const String &); public: String(const char *str = NULL); String(const String &); String & operator=(const String &); ~String(); private: char *m_data; };

类成员函数的实现:

String::~String() { if (m_data) delete[] m_data; } String::String(const char *str) { if (str == NULL) { m_data = new char[1]; m_data[0] = '\0'; } else { m_data = new char[strlen(str)+1]; strcpy(m_data, str); } } String::String(const String &other) { m_data = new char[strlen(other.m_data)+1]; strcpy(m_data, other.m_data);//String str;str.m_data; } String & String::operator=(const String &other) { if (this == &other) return *this; delete[] m_data; m_data = new char[strlen(other.m_data)+1]; strcpy(m_data, other.m_data); return *this; } ostream & operator<<(ostream &out, const String &s) { out << s.m_data; return out; } int main() { String str; String str2("hello"); String str3(str2); String str4 = str2; cout << str << "#" << endl; cout << str2 << endl; cout << str3 << endl; cout << str4 << endl; }

2. 编写一个标准strcpy函数

总分值为10,下面给出几个不同得分的答案

以下是2分程序片段:

void strcpy(char *dest, char *src) { while ((*dest++ = *src++) != '\0'); }

以下是4分程序片段:

//将源字符串加const void strcpy(char *dest, const char *src) { while ((*dest++ = *src++) != '\0'); }

以下是7分程序片段:

//对源地址和目的地址加非NULL断言 void strcpy(char *dest, const char *src) { assert((dest != NULL) && (src != NULL)); while ((*dest++ = *src++) != '\0'); }

以下是10分程序片段:

//为了实现链式操作,将目的地址返回 char *strcpy(char *dest, const char *src) { assert((dest != NULL) && (src != NULL)); if (dest == src) return dest; char *addr = dest; while ((*dest++ = *src++) != '\0'); return addr; }

可以清楚的看到,小小的strcpy竟然暗藏着这么多玄机,真不是盖的!需要多么扎实的基本功才能写一个完美的strcpy啊!

读者看了不同分值的strcpy版本,应该也可以写出一个10分的strlen函数了:

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

函数strcmp的实现:

int strcmp(const char *str1, const char *str2) { assert((str1 != NULL) && (str2 != NULL)); while (*str1 && *str2 && (*str1 == *str2)) { str1++; str2++; } return *str1 - *str2; }

3. 文件中有一组整数,要求从小到大排序后输出到另一个文件中

#include <iostream> #include <fstream> #include <vector> using namespace std; //simple bubble sort void sort(vector<int> &data) { int temp, n = data.size(); for (int i = 1; i < n; i++) for (int j = 0; j < n-i; j++) if (data[j] > data[j+1]) { temp = data[j]; data[j] = data[j+1]; data[j+1] = temp; } } int main() { ifstream in(".\\data.txt"); if (!in) { cout << "open file error!" << endl; exit(1); } int temp; vector<int> data; while (!in.eof()) { in >> temp; data.push_back(temp); } in.close(); sort(data); ofstream out(".\\result.txt"); if (!out) { cout << "create file error!" << endl; exit(1); } for (size_t i = 0; i < data.size(); i++) out << data[i] << " "; out.close(); }

4. 两个int型数据,不用任何的判断语句如 if、switch、?: 等,找出其中的大值

int max(int x, int y) { int buf[2] = {x, y}; unsigned int z = x - y; z >>= 31; return buf[z]; }

利用位运算,不过一个很大的正数和一个很小的负数比较可能会溢出。

转载于:https://www.cnblogs.com/li-chong/p/3268686.html

最新回复(0)