1.Iterator使用
设计准则
2. 不区分大小写的string
这儿主要是采用修改char_traists的方式来得到与标准库中string类似的类ci_string
实例代码如下:
View Code
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
struct ci_char_traits:public char_traits<char>{
static bool eq(char c1,char c2){
return toupper(c1)==toupper(c2);
}
static bool ne(char c1,char c2){
return toupper(c1)!=toupper(c2);
}
static bool lt(char c1,char c2){
return toupper(c1)!=toupper(c2);
}
static int compare(const char* s1,
const char* s2,
size_t n){
int i=0;
while(*s1&&*s2&&*s1++==*s2++&&i++<n);
if(i<n||*s1&&!*s2||!*s1&&*s2)
return 0;
else
return 1;
}
static const char* find(const char *s,int n,char a){
while(n-->0&&toupper(*s)!=toupper(a)){
++s;
}
return s;
}
};
typedef basic_string<char,ci_char_traits> ci_string;
int main(){
ci_string s="Abc";
cout<<s.c_str()<<endl;
ci_string s2="abc";
cout<<(s==s2)<<endl;
ci_string s3="aBc";
cout<<(s2==s3)<<endl;
}
#include <string>
#include <cctype>
using namespace std;
struct ci_char_traits:public char_traits<char>{
static bool eq(char c1,char c2){
return toupper(c1)==toupper(c2);
}
static bool ne(char c1,char c2){
return toupper(c1)!=toupper(c2);
}
static bool lt(char c1,char c2){
return toupper(c1)!=toupper(c2);
}
static int compare(const char* s1,
const char* s2,
size_t n){
int i=0;
while(*s1&&*s2&&*s1++==*s2++&&i++<n);
if(i<n||*s1&&!*s2||!*s1&&*s2)
return 0;
else
return 1;
}
static const char* find(const char *s,int n,char a){
while(n-->0&&toupper(*s)!=toupper(a)){
++s;
}
return s;
}
};
typedef basic_string<char,ci_char_traits> ci_string;
int main(){
ci_string s="Abc";
cout<<s.c_str()<<endl;
ci_string s2="abc";
cout<<(s==s2)<<endl;
ci_string s3="aBc";
cout<<(s2==s3)<<endl;
}
3. 具有最大可复用性的通用Containers
成员模板。拷贝构造函数和赋值操作符重载。模板构造函数
template <class T,size_t size>
class fixed_vector{
public:
typedef T* iterator;
typedef const T* const_iterator;
iterator begin(){
return v_;
}
iterator end(){
return v_+size;
}
const_iterator begin() const{
return v_;
}
const_iterator end()const{
return v_+size;
}
private:
T v_[size];
};
class fixed_vector{
public:
typedef T* iterator;
typedef const T* const_iterator;
iterator begin(){
return v_;
}
iterator end(){
return v_+size;
}
const_iterator begin() const{
return v_;
}
const_iterator end()const{
return v_+size;
}
private:
T v_[size];
};
下面给出了一个解答,这个解答是存在问题的:
template <class T,size_t size>
class fixed_vector{
public:
typedef T* iterator;
typedef const T* const_iterator;
fixed_vector(){}
template <class O,size_t osize>
fixed_vector(const fixed_vector<O,osize> &other){
copy(other.begin(),other.begin()+min(size,osize)
begin());
}
template <class O,size_t osize>
fixed_vector<T,size>&
operator=(const fixed_vector<O,osize>& other){
copy(other.begin(),other.begin()+min(size,osize)
begin());
return *this;
}
iterator begin(){ return v_; }
iterator end(){ return v_+size; }
const_iterator begin() const{ return v_; }
const_iterator end()const{ return v_+size; }
private:
T v_[size];
};
class fixed_vector{
public:
typedef T* iterator;
typedef const T* const_iterator;
fixed_vector(){}
template <class O,size_t osize>
fixed_vector(const fixed_vector<O,osize> &other){
copy(other.begin(),other.begin()+min(size,osize)
begin());
}
template <class O,size_t osize>
fixed_vector<T,size>&
operator=(const fixed_vector<O,osize>& other){
copy(other.begin(),other.begin()+min(size,osize)
begin());
return *this;
}
iterator begin(){ return v_; }
iterator end(){ return v_+size; }
const_iterator begin() const{ return v_; }
const_iterator end()const{ return v_+size; }
private:
T v_[size];
};
4. 临时对象
代码中可能存在让你意想不到的临时对象
5. 代码的复杂性
6. 类的设计
操作符重载的一些原则:
View Code
class Complex{
public:
explicit Complex(double real,double image=0):real_(real),
image_(image){}
Complex& operator+=(const Complex& other){
real_+=other.real_;
image_+=other.image_;
return *this;
}
Complex& operator++(){
++real_;
return *this;
}
const Complex operator++(int){
Complex temp(*this);
++*this;
return temp;
}
ostream& Print(ostream& os)const{
return os<<"("<<real_<<","<<image_<<")";
}
private:
double real_,image_;
};
const Complex operator+(const Complex& lhs,const Complex& rhs){
Complex ret(lhs);
ret+=rhs;
return ret;
}
ostream& operator<<(ostream& os,const Complex& c){
return c.Print(os);
}
public:
explicit Complex(double real,double image=0):real_(real),
image_(image){}
Complex& operator+=(const Complex& other){
real_+=other.real_;
image_+=other.image_;
return *this;
}
Complex& operator++(){
++real_;
return *this;
}
const Complex operator++(int){
Complex temp(*this);
++*this;
return temp;
}
ostream& Print(ostream& os)const{
return os<<"("<<real_<<","<<image_<<")";
}
private:
double real_,image_;
};
const Complex operator+(const Complex& lhs,const Complex& rhs){
Complex ret(lhs);
ret+=rhs;
return ret;
}
ostream& operator<<(ostream& os,const Complex& c){
return c.Print(os);
}
7. 虚函数相关问题
如果要使用虚函数机制,在派生类中不要改写对应函数的默认参数。
这一段代码存在很多问题:
8. C++中的主要内存区域
9. bool类型的4种替代方法