代码如下:
/*
* CCSet.h
* c++_common_codes
*
* Created by xichen on 12-1-21.
* Copyright 2012 cc_team. All rights reserved.
*
*/
#ifndef CC_SET_H
#define CC_SET_H
#include "ccVector.h"
#include <iostream>
// CCSet
template <class T>
class CCSet
{
public:
CCSet() { }
~CCSet() { }
CCSet<T>(const CCSet<T> & ccSet);
CCSet<T> & operator=(const CCSet<T> & ccSet);
public:
void add(const T & value);
void remove(const T & value);
bool exist(const T & value) const;
int size() const;
int length() const;
bool empty() const;
void reset(); // set the CCSet to empty
T at(int index) const;
public:
CCSet<T> getIntersectionWith(const CCSet<T> & anotherCCSet);
public:
void show() const;
private:
CCVector<T> v;
};
template <class T>
CCSet<T>::CCSet(const CCSet<T> & ccSet)
{
for(int i = 0; i < ccSet.size(); ++i)
{
v.push_back(ccSet.at(i));
}
}
template <class T>
CCSet<T> & CCSet<T>::operator=( const CCSet<T> & ccSet )
{
if(this == &ccSet)
return *this;
this->reset();
for(int i = 0; i < ccSet.size(); ++i)
{
v.push_back(ccSet.at(i));
}
return *this;
}
// it's a pity that the template impementations should be in header file.
template <class T>
T CCSet<T>::at( int index ) const
{
return v[index];
}
template <class T>
void CCSet<T>::add( const T & value )
{
if(!exist(value))
v.push_back(value);
}
template <class T>
void CCSet<T>::remove( const T & value )
{
#ifdef _WINDOWS
CCVector<Item>::iterator it;
#else
T *it;
#endif
for(it = v.begin(); it != v.end(); ++it)
{
if(*it == value)
break;
}
v.erase(it);
}
template <class T>
bool CCSet<T>::exist( const T & value ) const
{
#ifdef _WINDOWS
CCSet<T>::const_iterator it;
#else
T *it;
#endif
for(it = v.begin(); it != v.end(); ++it)
{
if(*it == value)
return true;
}
return false;
}
template <class T>
int CCSet<T>::size() const
{
return v.size();
}
template <class T>
int CCSet<T>::length() const
{
return v.size();
}
template <class T>
bool CCSet<T>::empty() const
{
return v.size() == 0;
}
template <class T>
void CCSet<T>::reset()
{
v.clear();
}
template <class T>
CCSet<T> CCSet<T>::getIntersectionWith( const CCSet<T> & anotherCCSet )
{
CCSet<T> aSet;
#ifdef _WINDOWS
CCSet<T>::const_iterator it;
#else
T *it;
#endif
for(it = v.begin(); it != v.end(); ++it)
{
for(int i = 0; i < anotherCCSet.size(); ++i)
{
if(*it == anotherCCSet.at(i))
aSet.add(*it);
}
}
return aSet;
}
template <class T>
void CCSet<T>::show() const
{
std::cout << "CCSet is ";
#ifdef _WINDOWS
CCSet<T>::const_iterator it;
#else
T *it;
#endif
for(it = v.begin(); it != v.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << "CCSet size is " << size();
std::cout << " , empty is " << empty();
std::cout << std::endl;
}
#endif
测试代码:
void ccTestCCSet()
{
#if 1 // test CCSet
CCSet<int> s;
// s.show();
CC_ASSERT(s.size() == 0, "not equal");
s.add(3);
// s.show();
CC_ASSERT(s.size() == 1, "not equal");
CC_ASSERT(s.at(0) == 3, "not equal");
s.add(2);
// s.show();
CC_ASSERT(s.size() == 2, "not equal");
CC_ASSERT(s.at(1) == 2, "not equal");
s.add(1);
// s.show();
CC_ASSERT(s.size() == 3, "not equal");
CC_ASSERT(s.at(2) == 1, "not equal");
// COUT_LINE(s.at(1))
// COUT_LINE(s.exist(3))
// COUT_LINE(s.exist(0))
CC_ASSERT(s.at(1) == 2, "not equal");
CC_ASSERT(s.exist(3) == 1, "not equal");
CC_ASSERT(s.exist(0) == 0, "not equal");
s.add(0);
// COUT_LINE(s.exist(3))
// COUT_LINE(s.exist(0))
// s.show();
CC_ASSERT(s.exist(3) == 1, "not equal");
CC_ASSERT(s.exist(0) == 1, "not equal");
s.remove(3);
// COUT_LINE(s.at(1))
// COUT_LINE(s.exist(3))
// COUT_LINE(s.exist(0))
// s.show();
CC_ASSERT(s.at(1) == 1, "not equal");
CC_ASSERT(s.exist(3) == 0, "not equal");
CC_ASSERT(s.exist(0) == 1, "not equal");
CCSet<int> tempSet;
tempSet.add(3);
tempSet.add(2);
tempSet.add(1);
tempSet.add(-1);
CCSet<int> interSecSet = s.getIntersectionWith(tempSet);
// interSecSet.show();
CC_ASSERT(interSecSet.size() == 2, "not equal");
CC_ASSERT(interSecSet.at(0) == 2, "not equal");
CCSet<int> tempSet1(s);
// tempSet1.show();
CC_ASSERT(tempSet1.size() == 3, "not equal");
CC_ASSERT(tempSet1.at(2) == 0, "not equal");
s.reset();
// s.show();
CC_ASSERT(s.size() == 0, "not equal");
#endif
}
运行结果:
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
OK!
注:
1、CCVector实现: http://blog.csdn.net/cxsjabcabc/article/details/7302423
2、CC_ASSERT宏的定义:
#ifdef _WINDOWS
#define CC_ASSERT(var, msg) \
{ \
CCString strTemp19870814_neverUsed; \
char line[32] = {0}; \
strTemp19870814_neverUsed += __FILE__, strTemp19870814_neverUsed += " Line:", itoa(__LINE__, line, 10), strTemp19870814_neverUsed += line; \
if(!(var)) COUT_2_VAR_ENDL(strTemp19870814_neverUsed.pointer(), (msg)) \
else COUT_LINE("OK!") \
}
#define CC_ASSERT_EQUAL(var1, var2, msg) \
{ \
CCString strTemp19870814_neverUsed; \
char line[32] = {0}; \
strTemp19870814_neverUsed += __FILE__, strTemp19870814_neverUsed += " Line:", itoa(__LINE__, line, 10), strTemp19870814_neverUsed += line; \
if((var1) != (var2)) COUT_2_VAR_ENDL(strTemp19870814_neverUsed.pointer(), (msg)) \
else COUT_LINE("OK!") \
}
#else
#define CC_ASSERT(var, msg) \
{ \
CCString strTemp19870814_neverUsed; \
char line[32] = {0}; \
strTemp19870814_neverUsed += __FILE__, strTemp19870814_neverUsed += " Line:", sprintf(line, "%d",__LINE__), strTemp19870814_neverUsed += line; \
if(!(var)) COUT_2_VAR_ENDL(strTemp19870814_neverUsed.pointer(), (msg)) \
else COUT_LINE("OK!") \
}
#define CC_ASSERT_EQUAL(var1, var2, msg) \
{ \
CCString strTemp19870814_neverUsed; \
char line[32] = {0}; \
strTemp19870814_neverUsed += __FILE__, strTemp19870814_neverUsed += " Line:", sprintf(line, "%d",__LINE__), strTemp19870814_neverUsed += line; \
if((var1) != (var2)) COUT_2_VAR_ENDL(strTemp19870814_neverUsed.pointer(), (msg)) \
else COUT_LINE("OK!") \
}
#endif
3、COUT_LINE和COUT_2_VAR_ENDL宏的定义:
#define COUT_LINE(str) std::cout << (str) << std::endl;
#define COUT_2_VAR_ENDL(str1, str2) \
std::cout << (str1) << " " << (str2) << std::endl;
微风不燥,阳光正好,你就像风一样经过这里,愿你停留的片刻温暖舒心。
我是程序员小迷(致力于C、C++、Java、Kotlin、Android、Shell、JavaScript、TypeScript、Python等编程技术的技巧经验分享),若作品对您有帮助,请关注、分享、点赞、收藏、在看、喜欢,您的支持是我们为您提供帮助的最大动力。
欢迎关注。助您在编程路上越走越好!