// TODO remove
#include <iostream>
#include <assert.h> 
/////////
#include <map>
template<typename K, typename V>
class interval_map {
	friend void IntervalMapTest();
public:
    ////////// TODO move
    V m_valBegin;
	std::map<K,V> m_map;
	//////////////
	
	
	// constructor associates whole range of K with val
	interval_map(V const& val)
	: m_valBegin(val)
	{}

	// Assign value val to interval [keyBegin, keyEnd).
	// Overwrite previous values in this interval.
	// Conforming to the C++ Standard Library conventions, the interval
	// includes keyBegin, but excludes keyEnd.
	// If !( keyBegin < keyEnd ), this designates an empty interval,
	// and assign must do nothing.
	void assign( K const& keyBegin, K const& keyEnd, V const& val ) {
        if (keyEnd < keyBegin)
            return;
        if (m_map.empty()) {
            if (val == m_valBegin)
                return;
            m_map.insert(std::make_pair(keyBegin, val));
            m_map.insert(std::make_pair(keyEnd, m_valBegin));
        }
        //gestion borne inf
        V valBornInf = (*this)[keyBegin];
        if (!(valBornInf == val)){
            /////////
            // 1 LOG 
            /////////
            const auto [it, success] = m_map.insert(std::make_pair(keyBegin, val)); 
            // if the key is already assigned.
            if (!success) {
                it.second = val;
            }
        }
        // gestion borne sup

        // gestion milieu
	}

	// look-up of the value associated with key
	V const& operator[]( K const& key ) const {
		auto it=m_map.upper_bound(key);
		if(it==m_map.begin()) {
			return m_valBegin;
		} else {
			return (--it)->second;
		}
	}
};

// Many solutions we receive are incorrect. Consider using a randomized test
// to discover the cases that your implementation does not handle correctly.
// We recommend to implement a test function that tests the functionality of
// the interval_map, for example using a map of int intervals to char.
class myK {
    public:
        int val;
        myK(int V) : val(V){}
        myK (const myK& M) : val(M.val) {}

        friend bool operator<(const myK& l, const myK& r)
        {
            return l.val < r.val; // keep the same order
        }
};

class myV {
    public:
        char c;
        myV(char C) : c(C){}
        myV (const myV& M) : c(M.c) {}

        friend bool operator==(const myV& l, const myV& r)
        {
            return l.c == r.c; // keep the same order
        }
};

int main() {
    interval_map <myK, myV> mymap('a');
    assert(mymap.m_map.empty());
    mymap.assign(1, 3, 'a');
    assert(mymap.m_map.empty());
    mymap.assign(1, 3, 'w');
    for (auto &p : mymap.m_map ) {
        std::cout << p.first.val << " : " << p.second.c << '\n';
    }
   return 0;
} 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}