C++包含任何类型的模板类对象的映射
C++ map containing any type of template class object
我正在编写一个代码,我们需要几种堆栈。比如stack<int>, stack<vector<int>>, stack<map<int, string>>, stack<vector<vector<int>>
等。
我有一个基类,其中包含一个虚函数foo(...参数列表...此基类用作多个派生类的父类,这些派生类的 foo() 适用于不同类型的堆栈。因此,派生 1 可能使用stack<int>
,而派生 2 可能在各自的 foo() 中使用stack<vector<vector<int>>
等等。
现在我正在将所有堆栈传递给所有 foo 函数。因此,每次要实现新堆栈时,我的 foo() 定义都必须在 base 以及所有派生类中再插入一个堆栈类型。
因此,例如,最初我只有一个派生类,我的基和派生类是这样的
class Base
{
public:
void foo(stack<int>) = 0;
}
class Derived : public Base
{
public:
void foo(stack<int>)
{
...
...
}
}
现在,当一个新的派生类出现时,假设使用stack<vector<int>>
.我需要将我的基本和所有派生类 foo() 更新为
void foo(stack<int> a, stack<vector<int> b)
{
....
....
}
相反,我现在想要实现的是我创建一个包含各种堆栈的StackWrapper
类,并且我可以使用不同的键处理不同的堆栈。
类似的东西
class StackWrapper
{
stack<int> derived1;
stack<vector<int>> derived2;
some-return-type operator[](char ch)
{
switch(ch)
{
case 'a': return derived1;break;
case 'b': return derived2;break;
}
}
}
通过这种方式,我只需要在这个堆栈包装器类中添加一个新的堆栈类型对象,然后在不同的派生类中使用类似stackWrapperObj['a']
或stackWrapperObj['b']
的东西 foo()。
有没有办法允许这样的类设计。
注意:- 我不希望在 foo() 中进行任何转换,例如返回void*
然后稍后转换它,而是我想以某种方式返回一个转换的对象,具体取决于我分配给不同堆栈类型的各种键。这样我就可以做这样的操作
stack<int> a = stackWrapperObject['a'];
stack<vector<int>> b = stackWrapperObject['b'];
为什么要费心尝试编写operator[]
?
struct StackWrapper
{
stack<int> a;
stack<vector<int>> b;
}
void Derived::foo (StackWrapper & stacks)
{
stack<int> a = stacks.a;
stack<vector<int>> b = stacks.b;
// ...
}
此时您可能不需要这些当地人,只需直接使用stacks.a
尝试使用访客模式: 使用 Caesar Shift 的基本非模板示例:
主.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "CaesarShift.h"
int main() {
std::string filename;
std::cout << "Please enter the name of the input file. ";
std::cin >> filename;
std::ifstream fileIn;
std::string text;
fileIn.open( filename );
if ( !fileIn.is_open() ) {
std::cout << "Failed to open file: " << filename << "." << std::endl;
}
fileIn >> text;
fileIn.close();
CaesarText caesarText;
caesarText.addText( text );
std::cout << "Contents of the Caesar Text before peforming a Caesar Shift:n"
<< caesarText << std::endl;
int amount = 0;
std::cout << "Please enter the amount to shift the text ";
std::cin >> amount;
std::cout << "Now performing the Caesar Shift: " << std::endl;
caesarShift( caesarText, amount );
std::cout << "Caesar Text after performing a Caesar shift:n"
<< caesarText << std::endl;
std::ofstream fileOut;
fileOut.open( std::string( "shifted_" + filename ) );
if ( !fileOut.is_open() ) {
std::cout << "Failed to open shifted_" << filename << std::endl;
}
fileOut << caesarText.shiftedText() << std::endl;
fileOut.close();
system( "PAUSE" );
return 0;
}
凯撒转移
#ifndef CAESAR_SHIFT_H
#define CAESAR_SHIFT_H
class CaesarText {
std::string _originalText;
std::string _shiftedText;
public:
CaesarText() = default;
explicit CaesarText( const std::string& text ) :
_originalText( text ) {}
void addText( const std::string& text ) {
_originalText = text;
}
std::string originalText() const {
return _originalText;
}
std::string shiftedText() const {
return _shiftedText;
}
friend void caesarShift( CaesarText& c, int amount );
friend std::ostream& operator<<( std::ostream& out, const CaesarText& ceasarText );
};
#endif // !CAESAR_SHIFT_H
凯撒转移.cpp
#include "CaesarShift.h"
#include <string>
#include <iostream>
#include <algorithm>
std::ostream& operator<<( std::ostream& o, const CaesarText& c ) {
o << "Original Text: " << c._originalText << "n";
o << "Shifted Text: " << c._shiftedText << "n";
return o;
}
void caesarShift( CaesarText& text, int amount ) {
// Bound amount to the number of characters in the alphabet
amount %= 26;
// Three Different Ways To Perform The Shift //
/*for ( std::size_t i = 0; i < text._originalText.length(); i++ ) {
char c = text._originalText[i] + amount;
text._shiftedText += c;
}*/
for ( auto& c : text._originalText ) {
text._shiftedText += c + amount;
}
/*std::transform( text._originalText.begin(), text._originalText.end(),
std::back_inserter( text._shiftedText ),
[amount]( unsigned char c ) -> unsigned char { return c + amount; }
);*/
}
输入:测试.txt
Hello
控制台和文件输出- 基于用户输入的移动量:
// Shift by 2
Original Text: Hello
Shifted Text: Jgnnq
// Shift by 3
Original Text: Hello
Shifted Text: Khoor
// Shift by 29 (29 % 26) = 3
Original Text: Hello
Shifted Text: Khoor
如果你看上面的类,它只存储两个字符串。它确实具有返回两个私有成员的构造函数和函数。将移位应用于CaesarText
的实现独立于类或容器本身。重载运算符也同样如此,可以轻松地将此对象发送到输出流。
在这种特殊情况下,函数caeserShift()
引用 CaesarText 和 int。现在这个独立的功能在class object
上执行实际operations or calculations
。这里唯一特别的是,caesarShift()
是CaesarText
的朋友,它使它能够直接访问成员,这样您就不必getters
并创建不需要的额外临时和副本。所以最终algorithm
caesarShift()
不是CeasarText
的成员,而是对他们进行操作。在带有容器和迭代器的标准库中始终使用相同的模式。
- 使用std::函数映射对象方法
- 使用"std::unordereded_map"映射到"std::list"对象
- 在c++中访问int到类对象的映射时出错
- 如何创建对象函数指针C++映射?
- 准确了解对象在内存中的映射方式
- 将包含不可复制对象的对插入到映射中
- 添加要映射的对象的唯一指针
- 打印对象的映射,其中另一个对象作为键
- 单行初始化映射中的C++对象
- 将大型对象存储在无序映射中是否效率低下
- 具有不同参数列表的模板对象的模板静态映射
- 当覆盖映射中的复杂对象键时,旧对象将被删除(C++)
- C++-将对象(如字符串)映射到表中成员函数的正确方法
- 您可以将C 对象映射到内存映射的外围设备吗?
- std::对象映射或对象指针
- 指针映射或堆栈对象映射
- 为什么c++对象映射在Mavericks和Mountain Lion上不同?
- 使用预先填充的对象映射实现工厂模式
- C++XML到C++对象映射库
- c++类构造函数自动将对象映射到事件处理程序