首页 > 编程语言 >c++ 集合类 CCSet简单实现

c++ 集合类 CCSet简单实现

时间:2024-06-01 10:29:42浏览次数:10  
标签:const CC c++ ASSERT CCSet 集合 OK strTemp19870814

   代码如下:

   

/*
 *  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等编程技术的技巧经验分享),若作品对您有帮助,请关注、分享、点赞、收藏、在看、喜欢,您的支持是我们为您提供帮助的最大动力。

欢迎关注。助您在编程路上越走越好!

标签:const,CC,c++,ASSERT,CCSet,集合,OK,strTemp19870814
From: https://blog.csdn.net/cxsjabcabc/article/details/7315492

相关文章

  • 【华为OD】D卷真题200分:篮球比赛 C++代码实现[思路+代码]
    【华为OD】2024年C、D卷真题集:最新的真题集题库C/C++/Java/python/JavaScript【华为OD】2024年C、D卷真题集:最新的真题集题库C/C++/Java/python/JavaScript-CSDN博客 JS、C、C++、Java、python代码实现:【华为OD】D卷真题200分:篮球比赛JavaScript代码实现[思路+代码]-CSD......
  • C++基础编程部分知识总结应用--图书管理系统
    C++基础编程部分知识总结应用–图书管理系统文章目录C++基础编程部分知识总结应用--图书管理系统1.代码结构和预处理命令2.定义图书结构体3.定义图书列表结构体4.后台控制函数5.显示主菜单6.添加图书函数7.查询图书函数8.显示所有图书函数9.修改图书信息函数10......
  • C++高级编程之——函数重载、内联、缺省参数、隐式转换
    C++函数的高级特性对比于C语言的函数,C++增加了重载(overloaded)、内联(inline)、const和virtual四种新机制。其中重载和内联机制既可用于全局函数也可用于类的成员函数,const与virtual机制仅用于类的成员函数。重载和内联肯定有其好处才会被C++语言采纳,但是不可以当成免......
  • 【面试宝典】30道C++ 基础高频题库整理(附答案背诵版)
    1.C和C++有什么区别?C++是C语言的超集(我看网上很多文章说这是不对的),这意味着几乎所有的C程序都可以在C++编译器中编译和运行。然而,C++引入了许多新的概念和特性,使得两种语言在一些关键点上有显著的区别。以下是C和C++的一些主要区别:面向对象编程:C++支持面向对象编程(OOP),包......
  • c++参数 使用笔记
    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站:前言–人工智能教程目录函数两个返回值:1.按值传递(PassbyValue)2.按引用传递(PassbyReference)3.按常量引用传递(PassbyConstReference)4.按指针传递(PassbyPoint......
  • 【C++修行之道】类和对象(二)类的6个默认成员函数、构造函数、析构函数
    目录一、类的6个默认成员函数二、构造函数2.1概念2.2特性2.2.5自动生成默认构造函数不进行显示定义的隐患:2.2.6自动生成的构造函数意义何在?两个栈实现一个队列2.2.7 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。2.4 一般......
  • 【C++修行之道】类和对象(三)拷贝构造函数
    目录一、 概念二、特征 正确的拷贝构造函数写法:拷贝函数的另一种写法 三、若未显式定义,编译器会生成默认的拷贝构造函数。四、编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗?深拷贝的写法:五、拷贝构造函数典型调用场景:六、总结:......
  • C++11中的新特性(2)
    C++111可变参数模板2emplace_back函数3lambda表达式3.1捕捉列表的作用3.2lambda表达式底层原理4包装器5bind函数的使用1可变参数模板在C++11之前,模板利用class关键字定义了几个参数,那么我们在编译推演中,我们就必须传入对应的参数,如下图函数模板所示(类模板也......
  • C++ 习题精选(1)
    这里写目录标题1.字符串相加2.字符串中的第一个唯一字符1.字符串相加题目描述:给定两个字符串形式的非负整数num1和num2,计算它们的和并同样以字符串形式返回。你不能使用任何內建的用于处理大整数的库(比如BigInteger),也不能直接将输入的字符串转换为整数形式。......
  • C++学习: 输入与输出
    一、输出使用cout函数,用<<分隔。注:需要头文件#include<iostream>,所有符号都为英文。例:#include<iostream>intmain(){cout<<"hello!"<<endl;//endl或'\n'换行return0;}输出结果:hello!endl就是endline(换行)的意思。二、输入一、普通输入......