[ create a new paste ] login | about

Link: http://codepad.org/WpLEvSez    [ raw code | output | fork ]

C++, pasted on Apr 9:
#include <iostream>
#include <vector>
#include <set>
using namespace std;

class Voice
{
	public:
		template <typename T>
		std::multiset<T> getAtribDim(const std::vector<Voice>& v, 
											std::string attr)
		{
			std::multiset<T> satrib;		//multiset
			for (std::vector<Voice>::const_iterator ci = v.begin();
					ci != v.end(); ++ci) {
				if (attr == "air_time") {
						satrib.insert(ci->air_time);		//int member name
				} else if (attr == "charged_amount") {
						satrib.insert(ci->charged_amount);		//double member name
					
				} else if (attr == "id_cell") {
						satrib.insert(ci->id_cell);		//string member name
						
				}
			}
			return satrib;
		}
		int air_time;				// 115
		double charged_amount;			// 864
		std::string id_cell;			// "cell a"
};

int main()
{
	Voice voice;
	vector<Voice> vvoice;
	voice.air_time = 115;
	voice.charged_amount = 864.0;
	voice.id_cell = "cell a";
	vvoice.push_back(voice);
	voice.air_time = 15;
	voice.charged_amount = 64.0;
	voice.id_cell = "cell b";
	vvoice.push_back(voice);

	multiset<int> m;
	//get air_time
	m = voice.getAtribDim<int>(vvoice, "air_time");
	multiset<int>::const_iterator ci;
	ci = m.begin();
	cout << "air_time" << endl;
	while (ci != m.end())
	{
		cout << *ci << endl;
		++ci;
	}
	/*
	//get charged_amount
	m = voice.getAtribDim<double>(vvoice, "charged_amount");
	ci = m.begin();
	cout << "charged_amount" << endl;
	while (ci != m.end())
	{
		cout << *ci << endl;
		++ci;
	}
	//get id_cell
	m = voice.getAtribDim<int>(vvoice, "id_cell");
	ci = m.begin();
	cout << "id_cell" << endl;
	while (ci != m.end())
	{
		cout << *ci << endl;
		++ci;
	}
	*/

}


Output:
1
2
3
4
5
6
cc1plus: warnings being treated as errors
t.cpp: In member function '__gnu_debug_def::multiset<T, std::less<_Key>, std::allocator<_CharT> > Voice::getAtribDim(const __gnu_debug_def::vector<Voice, std::allocator<Voice> >&, std::string) [with T = int]':
t.cpp:49:   instantiated from here
Line 19: warning: passing 'const double' for argument 1 to '__gnu_debug::_Safe_iterator<typename __gnu_norm::multiset<_Key, _Compare, _Alloc>::iterator, __gnu_debug_def::multiset<_Key, _Compare, _Allocator> > __gnu_debug_def::multiset<_Key, _Compare, _Allocator>::insert(const _Key&) [with _Key = int, _Compare = std::less<int>, _Allocator = std::allocator<int>]'
Line 22: error: no matching function for call to '__gnu_debug_def::multiset<int, std::less<int>, std::allocator<int> >::insert(const std::string&)'
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: