C++ Primer Plus(StringBad类的改进)
来源:互联网 发布:手机数据恢复精灵 编辑:程序博客网 时间:2024/06/06 02:47
#ifndef STRING1_H#define STRING1_H#include<iostream>using std::ostream;using std::istream;class String{private: char *str; int len; static int num_strings; static const int CINLIM = 80;//cin input limitpublic: String(const char *s); String(); String(const String &);//copy constructor ~String(); int length() const { return len; } String & operator=(const String &); String & operator=(const char*); char & operator[] (int i); const char & operator[] (int i) const; friend bool operator<(const String &st, const String &st2); friend bool operator>(const String &st, const String &st2); friend bool operator==(const String &st, const String &st2); friend ostream & operator<<(ostream & os, const String &st); friend istream & operator>>(istream & is, String &st); static int HowMany();};#endif // !STRING1_H
#include"stdafx.h"#include<cstring>#include"string1.h"using std::cin;using std::cout;int String::num_strings = 0;int String::HowMany(){ return num_strings;}String::String(const char *s){ len = std::strlen(s); str = new char[len + 1]; std::strcpy(str, s); num_strings++;}String::String(){ len = 4; str = new char[1]; str[0] = '\0'; num_strings++;}String::String(const String &st){ num_strings++; len = st.len; str = new char[len + 1]; std::strcpy(str, st.str);}String::~String(){ --num_strings; delete[] str;}String & String::operator=(const String &st){ if (this == &st) return *this; delete[] str; len = st.len; str = new char[len + 1]; std::strcpy(str, st.str); return *this;}String & String::operator=(const char *s){ delete[] str; len = std::strlen(s); str = new char[len + 1]; std::strcpy(str, s); return *this;}char & String::operator[](int i){ return str[i];}const char & String::operator[](int i)const//make sure thda data won't be changed{ return str[i];}bool operator<(const String &st1, const String &st2){ return (std::strcmp(st1.str, st2.str) < 0);}bool operator>(const String &st1, const String &st2){ return st2 < st1;}ostream & operator<< (ostream & os, const String & st){ os << st.str; return os;}istream & operator >> (istream &is, String & st){ char temp[String::CINLIM]; is.get(temp, String::CINLIM); if (is) st = temp; while (is && is.get() != '\n') continue; return is;}
// String.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include"string1.h"const int ArSize = 10;const int MaxLen = 81;int main(){ using std::cout; using std::cin; using std::endl; String name; cout << "<<Hi,waht's your name?\n>>"; cin >> name; cout << name << ",please enter up to " << ArSize << " short sayings <empty line to quit>:\n"; String sayings[ArSize]; char temp[MaxLen]; int i; for (i = 0; i < ArSize; i++) { cout << i + 1 << ": "; cin.get(temp, MaxLen); while (cin && cin.get() != '\n') continue; if (!cin || temp[0] == '\0') break; else sayings[i] = temp; } int total = i; if (total > 0) { cout << "Here are your sayings:\n"; for (i = 0; i < total; i++) cout << sayings[i][0] << ": " << sayings[i] << endl; int shortest = 0; int first = 0; for (i = 1; i < total; i++) { if (sayings[i].length() < sayings[shortest].length()) shortest = i; if (sayings[i] < sayings[first]) first = i; } cout << "Shortest saying:\n" << sayings[shortest] << endl; cout << "First alphabetically:\n" << sayings[first] << endl; cout << "This program used " << String::HowMany() << "String objects.Bye!\n"; } else cout << "No input! Bye!\n"; return 0;}
1 0
- C++ Primer Plus(StringBad类的改进)
- C++ Primer Plus(StringBad类的实现)
- c primer plus 14.13 奇特的声明
- C primer plus:指向函数的指针
- 来自<c primer plus >的随记
- c++primer plus第十六章-string类
- C Primer Plus学习 三十四 存储类
- C-PRIMER PLUS读书笔记
- C-PRIMER PLUS读书笔记
- C Primer Plus 读书笔记
- C Primer Plus学习计划
- C primer plus 一到五章
- 重读 c primer plus
- c primer plus note
- c primer plus chapter6
- c primer plus chapter6
- 《c primer plus》
- C Primer Plus学习
- app 被强杀后应用崩溃的解决思路
- Android小型知识点收录:studio快捷键
- linux下socket编程常用头文件
- mysql绿色版安装
- 微信小程序正式开放公测,将给创业者带来哪些机会?
- C++ Primer Plus(StringBad类的改进)
- SeekBar小demo
- OSG osgDB::readNodeFile 读取ply文件
- 用于图像去雾的优化对比度增强算法
- webview和swiperefreshLayout 的下拉冲突问题
- CMD文件及段的总结
- 大数相加(hdu1022)
- studio快捷键
- Scrapy安装介绍-windows版