0

私は未定義の型を介してイテレータを持っています:

 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>
4

2 に答える 2

3

内部ロジックを関数に入れ、その関数をオーバーロードします。

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);
    }
}
于 2013-04-20T03:46:51.523 に答える