Hi Team
I am trying to implement an assign function together with test case for this program using interval map in c++ my program somehow is compiling this below error. "
prog.cc:210:1: error: a template declaration cannot appear at block scope
template<typename K, typename V>
^~~~~~~~
prog.cc:243:1: error: 'namespace' definition is not allowed here
namespace unit_test {
^~~~~~~~~
prog.cc:257:12: error: a function-definition is not allowed here before '{' token
int main() {
^
prog.cc: In function 'void unit_test::IntervalMapTest()':
prog.cc:458:8: error: 'cout' is not a member of 'unit_test::std'
std::cout << "Unit test will run here." << std::endl;
^~~~
prog.cc:458:51: error: 'endl' is not a member of 'unit_test::std'
std::cout << "Unit test will run here." << std::endl;
^~~~
"
What I have tried:
#include <iostream>
#include <map>
#include <limits>
template<typename K, typename V>
class IntervalMap {
public:
IntervalMap(V const& val)
: m_map({{std::numeric_limits<K>::lowest(), val}})
{
}
void assign(K const& keyBegin, K const& keyEnd, V const& val) {
auto beginIter = m_map.lower_bound(keyBegin);
auto endIter = m_map.upper_bound(keyEnd);
V const& prevVal = (--beginIter)->second;
if (prevVal == val) {
m_map.erase(beginIter, endIter);
} else {
m_map.erase(beginIter, endIter);
m_map.emplace(keyBegin, val);
m_map.emplace(keyEnd, prevVal);
}
}
V const& operator[](K const& key) const {
return (--m_map.upper_bound(key))->second;
}
private:
std::map<K, V> m_map;
};
namespace unit_test {
void IntervalMapTest() {
std::cout << "Unit test will run here." << std::endl;
IntervalMap<int, char> imap('A');
imap.assign(1, 5, 'B');
imap.assign(3, 7, 'C');
imap.assign(8, 10, 'D');
imap.assign(1, 10, 'E');
for (int i = 0; i <= 10; ++i) {
std::cout << i << ": " << imap[i] << std::endl;
}
}
}
int main() {
unit_test::IntervalMapTest();
return 0;
}
// the program is given as this as a problem before implementation(instruction)
#include <iostream>
#include <algorithm>
#include <list>
#include <map>
#include <limits>
#include <iterator>
template<typename T>
class ThinkCellKey : std::numeric_limits<T>{
T v;
public:
ThinkCellKey( T const& val) {
v = val;
}
bool operator <(const ThinkCellKey& d) const {
return v < d.v;
}
ThinkCellKey operator ++(int) {
this->v++;
return *this;
}
ThinkCellKey operator --(int) {
this->v--;
return *this;
}
friend std::ostream & operator << (std::ostream &out, const ThinkCellKey<T> &c) {
out << c.v;
return out;
}
static const ThinkCellKey<T> lowest() {
return std::numeric_limits<T>::lowest();
}
};
template<typename K, typename V>
class interval_map {
std::map<K,V> m_map;
public:
interval_map( V const& val) {
m_map.insert(m_map.end(),std::make_pair(std::numeric_limits<K>::lowest(),val));
}
void assign( K const& keyBegin, K const& keyEnd, V const& val ) {
if (!(keyBegin < keyEnd))
return;
auto before_begin_bound = m_map.lower_bound(keyBegin);
if (before_begin_bound != m_map.begin()) {
--before_begin_bound;
}
auto insert_res = before_begin_bound->second == val ?
before_begin_bound : m_map.insert_or_assign(before_begin_bound, keyBegin, val);
auto end_interval = m_map.lower_bound(keyEnd);
if (end_interval != m_map.end() && end_interval->second == val)
end_interval++;
insert_res++;
m_map.erase(insert_res, end_interval);
if (end_interval == m_map.end())
this->assign(keyEnd, std::numeric_limits<K>::max(), before_begin_bound->second);
else
this->assign(keyEnd, end_interval->first, before_begin_bound->second);
}
V const& operator[]( K const& key ) const {
return ( --m_map.upper_bound(key) )->second;
}
void show() {
std::cout << "show" << std::endl;
for(auto entry : m_map) {
std::cout << entry.first << entry.second << std::endl;
}
}
};
int main(int argc, char* argv[])
{
interval_map<unsigned int, char> imap {'A'};
imap.assign(8, 12, 'k');
imap.assign(2, 12, 'k');
imap.assign(2, 12, 'b');
imap.assign(5, 12, 'b');
imap.assign(4, 10, 'b');
imap.assign(4, 12, 'b');
imap.assign(8, 13, 'a');
imap.assign(6, 9, 'j');
imap.show();
return 0;
}