我有以下MyClass包含 2D 地圖(std::map<std::string, std::map<std::string,double>>)的類。
我想知道是否可以實作MyClass::begin() MyClass::end()for 函式MyClass,以便有一個基于范圍的 for 回圈(如下面的代碼所示),這將允許我列印該 2D 地圖中包含的所有雙精度數。
需要明確的是,我不想引入雙 for 回圈,我想要一個 for() 回圈
(之后的目標是將地圖mp作為私有成員,MyClass并且只允許通過基于范圍的 for 回圈遍歷類)
提前謝謝了!
#include<string>
#include<map>
#include<iostream>
class MyClass{
public:
MyClass() {} ;
~MyClass(){};
std::map<std::string, std::map<std::string,double>> mp = {};
};
int main()
{
MyClass mycls ;
mycls.mp["a"]["a"] = 1;
mycls.mp["a"]["b"] = 2;
mycls.mp["a"]["c"] = 3;
mycls.mp["b"]["a"] = 4;
mycls.mp["b"]["b"] = 5;
mycls.mp["b"]["c"] = 6;
mycls.mp["d"]["a"] = 7;
mycls.mp["d"]["b"] = 8;
mycls.mp["d"]["c"] = 9;
mycls.mp["e"]["a"] = 10;
mycls.mp["e"]["b"] = 11;
mycls.mp["e"]["c"] = 12;
for (std::pair<const std::string, double> &obj : mycls){
std::cout << "obj.second = " << obj.second << std::endl;
}
return 0 ;
}
uj5u.com熱心網友回復:
是的,可以做到。您必須引入迭代器功能。
如果您在此處查看 CPP 參考,那么您可以在說明中看到基于范圍的 for 回圈的要求。只需要 5 個函式。
你的類必須有一個
begin()回傳自定義迭代器的函式你的類必須有一個
end()回傳自定義迭代器的函式你的類必須有一個自定義迭代器和一個取消參考它的函式
你的類必須有一個自定義迭代器和一個遞增它的函式
你的類必須有一個自定義迭代器和一個用于比較的函式
自定義迭代器的實作非常簡單。
你寫 5 using 陳述句來滿足形式要求。然后,您需要定義迭代器的內部表示,例如指標或其他迭代器。
在下面的示例中,我們重用了嵌套映射中的 2 個迭代器。外部映射的迭代器和內部映射的互動器。為了更容易實作功能,我們還將存盤一個指向周圍自定義類的指標。
遞增(和遞減操作)需要更多的努力,因為我們需要處理內部映射迭代器的環繞。
示例:如果內部迭代器到達末尾,我們必須遞增外部迭代器,然后將內部迭代器重置到開始位置。
我還添加了一個面臨類似挑戰的遞減函式。
通過添加一個差異函式,我通過破壞字串與雙精度的關系,使整個事情變得可排序。所以,不要那樣做。
但是有了這個功能,我們可以很容易地實作類似太空船的迭代器比較。
請參閱下面的許多潛在解決方案之一。為了您的方便,我添加了更多功能。
#include<string>
#include<map>
#include<iostream>
#include <iterator>
#include <algorithm>
#include <numeric>
using Map = std::map<std::string, double>;
using MapMap = std::map<std::string, Map>;
struct MyClass {
struct iterator; // Forward declaration
MapMap mp{};
bool empty() { return mp.empty(); }
size_t size() { return std::accumulate(mp.begin(), mp.end(), 0u, [](const size_t& sum, const auto& m) { return sum m.second.size(); }); }
iterator begin() { return iterator(&mp, mp.begin()->second.begin(), mp.begin()); }
iterator end() { return iterator(&mp, ((--mp.end())->second).end(), mp.end()); }
// iterator stuff ---------------------------------------------------------------------------
struct iterator {
// Definitions ----------------
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = double;
using pointer = double*;
using reference = double&;
// Data
MapMap* outerMapPtr{}; // Reference to surrounding nested map
MapMap::iterator outerMapIterator{}; // Iterator to the outer part of the nesed map
Map::iterator innerMapIterator{}; // Iterator to the outer part of the nesed map
// Constructor for iterator. Take a pointer to the surrounding nested map and both iterators
iterator(MapMap* const mmSD, const Map::iterator& msdIter, const MapMap::iterator& mmsdIter) :
outerMapPtr(mmSD), innerMapIterator(msdIter), outerMapIterator(mmsdIter) {}
// Dereferencing
reference operator *() const { return innerMapIterator->second; }
reference operator->() const { return **this; }
// Comparison. Must be template, because the other iterator may be of different type
template <typename Iter>
bool operator != (const Iter& other) const { return(outerMapIterator != other.outerMapIterator) or (innerMapIterator != other.innerMapIterator); }
template <typename Iter>
bool operator == (const Iter& other) const { return(outerMapIterator == other.outerMapIterator) and (innerMapIterator == other.innerMapIterator); }
bool operator < (const iterator& other) const { return other - *this < 0; };
bool operator <= (const iterator& other) const { return other - *this <= 0; };
bool operator > (const iterator& other) const { return other - *this > 0; };
bool operator >= (const iterator& other) const { return other - *this >= 0; };
// Arithmetic operations
// A little bit complex
iterator operator () {
// If we are at the end with the outer iterator, we do nothing
if (outerMapPtr->empty() or (outerMapIterator != outerMapPtr->end())) {
// We want to increment the inner iterator. Before we do that, we check, if this is already at the end
if (innerMapIterator != outerMapIterator->second.end())
innerMapIterator;
// So, now the innerMapIterator can be at the end, either from the beginning or after incrementing it
if (innerMapIterator == outerMapIterator->second.end()) {
// Increment outer iterator
outerMapIterator;
// And reset the inner interator back to begin, but only if the outer iterator is not at the end now
if (outerMapIterator != outerMapPtr->end())
innerMapIterator = outerMapIterator->second.begin();
}
}
return *this;
}
iterator operator --() {
// No decrementation on empty container
if (not outerMapPtr->empty()) {
// If we are at the end of the outer iterator then decrement it
if (outerMapIterator == outerMapPtr->end())
--outerMapIterator;
// If we are not at the begin the inner iterator
if (innerMapIterator != outerMapIterator->second.begin())
--innerMapIterator;
else {
// Inner iterator was at begin, therefore also decrement outer one
if (outerMapIterator != outerMapPtr->begin()) {
--outerMapIterator;
innerMapIterator = outerMapIterator->second.end();
if (innerMapIterator != outerMapIterator->second.begin())
--innerMapIterator;
}
}
}
return *this;
}
// Derived functions
iterator operator (int) { iterator tmp = *this; * this; return tmp; }
iterator operator--(int) { iterator tmp = *this; --* this; return tmp; }
iterator operator (const difference_type& n) const {
iterator temp{ *this }; difference_type k{ n }; if (k > 0) while (k--) temp; else while (k )--temp; return temp;
}
iterator operator =(const difference_type& n) {
difference_type k{ n }; if (k > 0) while (k--) * this; else while (k )--* this; return *this;
};
iterator operator -(const difference_type& n) const {
iterator temp{ *this }; difference_type k{ n }; if (k > 0) while (k--)--temp; else while (k ) temp; return temp;
}
iterator operator -=(const difference_type& n) {
difference_type k{ n }; if (k > 0) while (k--)--* this; else while (k ) * this; return *this;
};
// Difference. Very inefficient
difference_type operator-(const iterator& other) const {
difference_type result{};
// No subtraction on empty container
if (not outerMapPtr->empty()) {
int indexThis{ }, indexOther{ }, index{};
iterator current(outerMapPtr, outerMapPtr->begin()->second.begin(), outerMapPtr->begin());
iterator last(outerMapPtr, ((--outerMapPtr->end())->second).end(), outerMapPtr->end());
for (; current != last; current, index) {
if (current == *this)
indexThis = index;
if (current == other)
indexOther = index;
}
if (current == *this)
indexThis = index;
if (current == other)
indexOther = index;
if (indexThis >= 0 and indexOther >= 0)
result = indexThis - indexOther;
}
return result;
}
};
};
int main()
{
MyClass mycls;
mycls.mp["a"]["a"] = 1;
mycls.mp["a"]["b"] = 2;
mycls.mp["a"]["c"] = 3;
mycls.mp["b"]["a"] = 4;
mycls.mp["b"]["b"] = 5;
mycls.mp["b"]["c"] = 6;
mycls.mp["d"]["a"] = 7;
mycls.mp["d"]["b"] = 8;
mycls.mp["d"]["c"] = 9;
mycls.mp["e"]["a"] = 10;
mycls.mp["e"]["b"] = 11;
mycls.mp["e"]["c"] = 12;
std::cout << "\nSize: " << mycls.size() << "\n\n";
for (double d : mycls)
std::cout << d << ' ';
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425956.html
上一篇:與C 11函式簽名不一致
