c++ - 如何确定我输入的是什么类型

标签 c++

我通过未定义类型获得了迭代器:

 for (typename Type::const_iterator hayStackIterator = hayHeap.begin(); hayStackIterator != hayHeap.end(); ++hayStackIterator) {
           //some inner logic
}

如果知道我的 *hayStackIterator 是什么类型,以便能够根据此信息修改内部逻辑,那就太好了……是否有一些简单的函数可以实现这样的功能?

if (*hayStackIterator.isInstanceOf(vector<string>){
//do something
} else if (*hayStackIterator.isInstanceOf(string){
//do something else
}

我可以使用这些 includes:

#include <cctype>
#include <iostream>
#include <iomanip>
#include <set>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <string>

最佳答案

将内部逻辑放在一个函数中,并重载该函数:

void innerLogic(vector<string> const& vec) {
    //do something
}

void innerLogic(string const& str) {
    //do something else
}

void loop() {
    for (typename Type::const_iterator hayStackIterator = hayHeap.begin();
         hayStackIterator != hayHeap.end();
         ++hayStackIterator)
    {
        innerLogic(*hayStackIterator);
    }
}

关于c++ - 如何确定我输入的是什么类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16116327/

相关文章:

c++ - 如何在 Poco 中设置在内核跟踪/ps/gdb 中调试可见的单个线程名称?

c++ - 当它们应用于构造函数时,括号初始值设定项列表中是否有序列点?

c++ - Project Euler #1 测试扩展

c++ - QT 项目文件 - 字段含义

c++ - Qt 5.2 调用 QTableWidget::update 不会导致 QTableWidget::paintEvent

c++ - 二维 HashMap : A matrix where row and column types are pointers

c++ - 将一个像素从颜色空间转换到另一个

c++ - 链接到 C++ 库的 Makefile

C++ ifstream从linux到arduino

c++ - xtgmath.h 有编译时错误