# Sum function between two containers

Page 1 of 1

## 1 Replies - 698 Views - Last Post: 01 July 2012 - 10:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=284509&amp;s=5317ce696efd3a07f696295755d4fc0a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 leeto

Reputation: -3
• Posts: 38
• Joined: 17-June 12

# Sum function between two containers

Posted 01 July 2012 - 09:33 PM

I need to create function Sum() that calculates sum between two containers. Code below work fine except function Sum between two containers...

How I should re - write my code that everything work fine. Many thanks in advance.

Condition of exercise is : "Also create a Sum() function that calculates the sum between two iterators. The function then uses the template argument for the iterator type and accepts two iterators, the start- and end iterator"

1>------ Build started: Project: HP2_ex2_iter, Configuration: Debug Win32 ------
1> main.cpp
1>c:\all my\с++\ha level 7\solution\level 7 homework overview of the standard template library\hp2_ex2_iter\main.cpp(47): error C2275: 'C1' : illegal use of this type as an expression
1> c:\all my\с++\ha level 7\solution\level 7 homework overview of the standard template library\hp2_ex2_iter\main.cpp(107) : see declaration of 'C1'
1> c:\all my\с++\ha level 7\solution\level 7 homework overview of the standard template library\hp2_ex2_iter\main.cpp(107) : see reference to function template instantiation 'double Sum<std::vector<_Ty>,std::list<_Ty>>(C1,C2)' being compiled
1> with
1> [
1> _Ty=int,
1> C1=std::vector<int>,
1> C2=std::list<int>
1> ]
1>c:\all my\с++\ha level 7\solution\level 7 homework overview of the standard template library\hp2_ex2_iter\main.cpp(47): error C2275: 'C2' : illegal use of this type as an expression
1> c:\all my\с++\ha level 7\solution\level 7 homework overview of the standard template library\hp2_ex2_iter\main.cpp(107) : see declaration of 'C2'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

```#include <list>
#include <vector>
#include <iostream>
#include <map>
using namespace std;

template <typename T>
double Sum (list<T>& container)
{
double sum = 0;
list<T>::iterator il=container.begin();
cout << endl;
while (il!=container.end())
{
sum+=*(il++);
}
return sum ;
}

template <typename T>
double Sum (vector<T>& container)
{
double sum = 0;
vector<T>::iterator iv=container.begin();
while (iv!=container.end())
{
sum+=*(iv++);
}
return sum;
}

template <typename T, typename T1>
double Sum (map<T, T1>& container)
{
double sum =0 ;
map<T, T1>::iterator im=container.begin();
while (im!=container.end())
{
sum+=im++->second;
}
return sum;
}

template <typename C1,typename C2>
double Sum (C1, C2)
{
return Sum(C1) + Sum(C2); // line 47
}

int main() {

//*****************
// LIST CONTAINER
//*****************
cout << "----- sum of list container ----- "<< endl;

list <int> l ;
for (int i=0;i<10;i++) l.push_back(i);
list<int>::iterator il=l.begin();

cout << "-----Print without accumlating------" << endl;
for (il=l.begin();il!=l.end();++il)
{
cout << (*il) << ", ";
}
cout << endl;
cout << "\ntest of sum function for list :" << Sum(l)  << endl;

//*****************
// VECTOR CONTAINER
//*****************
cout << "\n\n\n\n----- sum of VECTOR CONTAINER ----- "<< endl;
double sumV=0;

vector <int> v;
for (int i=0;i<10;i++) v.push_back(i);
vector<int>::iterator iv=v.begin();

cout << "-----Print without accumlating------" << endl;
for (iv=v.begin();iv!=v.end();++iv)
{
cout << (*iv) << ", ";
}
cout << "\ntest of sum function for vector  :"<< 	Sum(v)  << endl;

//*****************
// MAP CONTAINER
//*****************
cout << "\n\n\n\n----- sum of MAP CONTAINER ----- "<< endl;
double sumM=0;

map <double, double > m;
for (int i=0;i<10;i++) m[i] = i*10;
map<double, double>::iterator im=m.begin();

cout << "\ntest of sum function for map  :"<< 	Sum(m)  << endl;

cout << "-----Print without accumlating------" << endl;
for (im=m.begin();im!=m.end();++im)
{
cout <<"("<< im->first << ", " << im->second<<")";
}

cout <<endl;

//*****************
// Sum of two CONTAINERs
//*****************

cout << "\ntest of sum function for sum between two containers  :" <<Sum(v,l) <<  endl; // line 107

return 0;
}
```

Is This A Good Question/Topic? 1

## Replies To: Sum function between two containers

### #2 Salem_c

• void main'ers are DOOMED

Reputation: 1939
• Posts: 3,824
• Joined: 30-May 10

## Re: Sum function between two containers

Posted 01 July 2012 - 10:46 PM

044 template <typename C1,typename C2>
045 double Sum (C1, C2)
046 {
047 return Sum(C1) + Sum(C2); // line 47
048 }
Well you need variable names as well, see your two previous sum functions.

Eg.
```template <typename C1,typename C2>
double Sum (C1 &c1, C2 &c2)
{
return Sum(c1) + Sum(c2); // line 47
}

```