Форум программистов, компьютерный форум CyberForum.ru

STL Iterators - C++

Восстановить пароль Регистрация
 
 
Рейтинг: Рейтинг темы: голосов - 12, средняя оценка - 4.92
Leeto
 Аватар для Leeto
7 / 7 / 0
Регистрация: 23.12.2011
Сообщений: 372
Записей в блоге: 1
07.08.2012, 19:13     STL Iterators #1
Допустим есть проект

ContainerTemplateFunction_hpp
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef ContainerTemplateFunction_hpp   // Preprocessor gates
#define ContainerTemplateFunction_hpp
    
        #include <list>
        #include <vector>
        #include <iostream>
        #include <map> 
 
        template <typename T> 
        double Sum (std::list<T>& MyList)
            {
 
                double sum_of_MyList_elements = 0;
 
                std::list<T>::iterator iMyListIterator = MyList.begin();    
                
                
                    while (iMyListIterator != MyList.end()) // A loop that iterates MyList 
                        {
                            sum_of_MyList_elements += *(iMyListIterator++);
                        }
                return sum_of_MyList_elements ; 
            }
 
 
 
        template  <typename T> 
        double Sum (std::vector<T>& MyVector)
            {
                double sum_of_MyVector_elements = 0;
 
                std::vector<T>::iterator iMyVectorIterator = MyVector.begin();  
 
                    while (iMyVectorIterator!=MyVector.end())
                        {
                            sum_of_MyVector_elements += *(iMyVectorIterator++);
                        }
                return sum_of_MyVector_elements;
            }
 
        template <typename T, typename T1> 
        double Sum ( std::map<T, T1>& MyMap)
            {
                double sum_of_MyMap_elements =0 ;
 
                std::map<T, T1>::iterator iMyMapIterator=MyMap.begin();
 
                    while (iMyMapIterator!=MyMap.end())
                        {
                            sum_of_MyMap_elements += iMyMapIterator++ -> second;
                        }
                return sum_of_MyMap_elements;
            }
 
            template <typename FirstSummandContainer,typename SecondSummandContainer>
 
            double Sum (FirstSummandContainer& ObjFirstSummandContainer, 
                SecondSummandContainer& ObjSecondSummandContainer)
                {
                    return Sum(ObjFirstSummandContainer) + Sum(ObjSecondSummandContainer);
                }
 
 
 
 
#endif


main.cpp
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "ContainerTemplateFunction.hpp"
 
int main() 
    {
 
        std::cout   << "\n\n\t\t    --- TEST OF SUM --- " 
                            << "\n\t\t     --- *LIST* --- \n " ;
 
 
        std::cout << "----- sum of list container ----- "<< std::endl;
 
        std::list <int> My_TestSum_List ; 
        for (int i=0;i<10;i++) // filling <list > of elements 
            {
                My_TestSum_List.push_back(i);
            }
 
        std::list<int>::iterator iMy_List_Iterator = My_TestSum_List.begin();
        
        std::cout << "-----Print without accumlating------" << std::endl;
            for (iMy_List_Iterator = My_TestSum_List.begin();
                 iMy_List_Iterator != My_TestSum_List.end() ; ++iMy_List_Iterator)
                {
                    std::cout << (*iMy_List_Iterator) << ", ";
                }
            std::cout << std::endl;
            std::cout << "\ntest of sum function for list :" << Sum(My_TestSum_List)  << std::endl;
 
// __________________________________________________________________________________________
 
            std::cout   << "\n\n\t\t    --- TEST OF SUM --- " 
                            << "\n\t\t      --- *VECTOR* --- \n " ;
        
        std::vector <int> My_TestSum_Vector;
 
            for (int i=0;i<10;i++) 
                {
                    My_TestSum_Vector.push_back(i);
                }
 
        std::vector<int>::iterator iMy_Vector_Iterator = My_TestSum_Vector.begin();
    
            std::cout << "-----Print without accumlating------" << std::endl;
            for (iMy_Vector_Iterator  = My_TestSum_Vector.begin() ; 
                 iMy_Vector_Iterator != My_TestSum_Vector.end(); ++iMy_Vector_Iterator)
                {
                    std::cout << (*iMy_Vector_Iterator) << ", ";
                }
            std::cout << "\ntest of sum function for vector  :"<<   Sum(My_TestSum_Vector)  << std::endl;
// __________________________________________________________________________________________
 
            std::cout       << "\n\n\t\t    --- TEST OF SUM --- " 
                            << "\n\t\t       --- *MAP* --- \n " ;
    
    
        std::map <double, double > My_SumTest_Map;
 
        for (int i=0;i<10;i++) My_SumTest_Map[i] = i*10;
 
        std::map<double, double>::iterator iMy_Map_Iterator = My_SumTest_Map.begin();
    
        std::cout << "\n sum of all MyMap elements  is :"   <<  Sum(My_SumTest_Map) ;
 
        std::cout << "\n-----Print without accumlating------" << std::endl;
 
            for (iMy_Map_Iterator = My_SumTest_Map.begin();
                iMy_Map_Iterator!=My_SumTest_Map.end(); ++iMy_Map_Iterator)
//              for (auto)
                {
                    std::cout   << "\n["    << iMy_Map_Iterator->first  
                                << "] -> " 
                                << "("      << iMy_Map_Iterator->second<<")";
                }
 
            
// __________________________________________________________________________________________
            std::cout   << "\n\n\t\t    --- TEST OF SUM --- " 
                            << "\n\t\t   --- *TWO CONTAINER.* --- \n " ;
 
            std::cout   << "\n Sum between two containers <vector> and <map> is :" 
                        <<Sum(My_TestSum_Vector,My_SumTest_Map) 
 
                        << "\n Sum between two containers <vector> and <list> is :" 
                        <<Sum(My_TestSum_Vector,My_TestSum_List) 
 
                        << "\n Sum between two containers <list> and <map> is :" 
                        <<Sum(My_TestSum_List,My_SumTest_Map) ; 
                        
 
            std::cout << "\n\n\n\t"; 
        return 0;
    }


Хочется чтобы
в ContainerTemplateFunction.hpp было не 3 шаблона для каждого контейнера отдельно, а один шаблон...
как сделать совместный шаблон для вектора и листа более менее понятно как сделать чтобы для карты (мап ) (map) вместе с вектором и листом был один шаблон (ну типа там же 2 аргумента а не один для как для листа и вектора ) подскажите плиз )
Заранее спасибо !
Similar
Эксперт
41792 / 34177 / 6122
Регистрация: 12.04.2006
Сообщений: 57,940
07.08.2012, 19:13     STL Iterators
Посмотрите здесь:

C++ STL
STL C++
iterators & file C++
C++ STL
[C++]Problems with iterators C++
После регистрации реклама в сообщениях будет скрыта и будут доступны все возможности форума.
Leeto
 Аватар для Leeto
7 / 7 / 0
Регистрация: 23.12.2011
Сообщений: 372
Записей в блоге: 1
13.08.2012, 21:45  [ТС]     STL Iterators #21
Цитата Сообщение от ForEveR Посмотреть сообщение
Как вариант.
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <vector>
#include <map>
 
template<typename T>
T get_el(const T& value)
{
    return value;
}
 
template<typename T, typename T2>
T2 get_el(const std::pair<const T, T2>& value)
{
    return value.second;
}
 
template<typename Container>
double sum(const Container& cnt)
{
   typedef typename Container::const_iterator c_iter;
   double sum = 0;
   for (c_iter i = cnt.begin();
        i != cnt.end();
        ++i)
   {
      sum += get_el(*i);
   }
   return sum;
}
 
template<typename Iterator>
double sum(Iterator first, Iterator last)
{
    double sum = 0;
    for (; first != last; ++first)
    {
        sum += get_el(*first);
    }
    return sum;
}
 
int main()
{
   double array[] = {1, 2};
   std::vector<double> v(array, array + sizeof(array) / sizeof(*array));
   std::cout << sum(v) << std::endl;
   std::pair<int, double> map[] = 
   {
     std::make_pair(1, 1.0),
     std::make_pair(2, 2.0)
   };
   std::map<int, double> m(map, map + sizeof(map) / sizeof(*map));
   std::cout << sum(m) << std::endl;
   std::cout << "By iters " << sum(m.begin(), m.end()) << std::endl;
}
main.cpp
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <map>
 
 
template<typename T>
T GetElement(const T& value)
{
    return value;
}
 
template<typename T, typename T2>
T2 GetElement(const std::pair<const T, T2>&  value) 
    {   
        return value.second;    
    }
 
 
 
template<typename T> 
 
double Sum(const T& MyContainer)
{   
   typedef typename T::const_iterator c_iter;
   double Sum = 0;
   for (c_iter MyConstIterator = MyContainer.begin(); MyConstIterator != MyContainer.end(); ++MyConstIterator)
       { 
          Sum += GetElement(*MyConstIterator);
       }
   return Sum; /
}
 
 
 
template<typename Iterator>
double Sum(Iterator myFirstIterator, Iterator mySecondIterator)
{
    double Sum = 0;
    for (; myFirstIterator != mySecondIterator; ++myFirstIterator)
        {
            Sum += GetElement(*myFirstIterator);
        }
    return Sum;
}
 
int main ()
{
 
    const unsigned int size_of_data_array = 35; 
 
        double data_doubles[size_of_data_array] 
                    =  { 1830 , 190.1, 1914. , 1863 , 1839 ,        // 0 - 4
                          } ;       
 
 
    
    
    
        std::map <int, double > My_SumTest_Map;
 
        for (int i=0;i<10;i++) My_SumTest_Map[i] = i*10.5;
 
        std::map<std::string, double > MapStringDouble;
        
        MapStringDouble ["The Wireless"] = data_doubles[1];
        MapStringDouble ["WWI"] = data_doubles[2];
        MapStringDouble ["Alfred Nobel"] = data_doubles[3];
 
        std::map<std::string, double>::const_iterator iMy_Map_Iterator = MapStringDouble.cbegin();
 
        //std::map<int, double>::const_iterator iMy_Map_Iterator = My_SumTest_Map.cbegin();
    
        std::cout << "\n-----Print without accumlating------" << std::endl;
 
        for (iMy_Map_Iterator; iMy_Map_Iterator != MapStringDouble.cend(); ++iMy_Map_Iterator)
                {
                    std::cout   << "\n["    ; iMy_Map_Iterator->first   ;
                    std::cout       << "] -> " 
                                << "("      << iMy_Map_Iterator->second<<")";
                }
 
 
            //std::cout << "\n Sum of all MyMap elements   is  :  " <<  Sum(My_SumTest_Map) ;
        std::cout << "\n Sum of all MyMap elements   is  :  "   <<  Sum(MapStringDouble) ;
            std::cout << "\n\n\n";
return 0;
}


Ребят подскажите в чем дело если делать map <int,double > то все ок если делать std::map<std::string, double> то все как то не радужно... вылезают какие ошибки связанные с const и все такое

вот список ошибок
errors

1>------ Build started: Project: L7_ex2_map_string_problem, Configuration: Debug Win32 ------
1> main.cpp
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\list(1588) : see declaration of 'std::operator <'
1> c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(124) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1> with
1> [
1> _Ty=std::string
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\map(71) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1> with
1> [
1> _Ty=std::string
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(451) : see reference to class template instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=double,
1> _Pr=std::less<std::string>,
1> _Alloc=std::allocator<std:air<const std::string,double>>,
1> _Mfl=false
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(520) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,double,std::less<std::string>,std::allocator<std:air<const std::string,double>>,false>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(659) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,double,std::less<std::string>,std::allocator<std:air<const std::string,double>>,false>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\map(81) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,double,std::less<std::string>,std::allocator<std:air<const std::string,double>>,false>
1> ]
1> c:\all my\с++\lab_for_level_7_8\l7_ex2_map_string_problem\main.cpp(63) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=double
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\list(1588) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\list(1588) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(1885) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(1885) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(1885) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\vector(1502) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\vector(1502) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\vector(1502) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\memory(2582) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\memory(2582) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\memory(2582) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1356) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1356) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1356) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1179) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1179) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1179) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std:air<_Ty1,_Ty2> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\utility(318) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std:air<_Ty1,_Ty2> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\utility(318) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std:air<_Ty1,_Ty2> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\utility(318) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Добавлено через 17 минут
Цитата Сообщение от Leeto Посмотреть сообщение
main.cpp
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <map>
 
 
template<typename T>
T GetElement(const T& value)
{
    return value;
}
 
template<typename T, typename T2>
T2 GetElement(const std::pair<const T, T2>&  value) 
    {   
        return value.second;    
    }
 
 
 
template<typename T> 
 
double Sum(const T& MyContainer)
{   
   typedef typename T::const_iterator c_iter;
   double Sum = 0;
   for (c_iter MyConstIterator = MyContainer.begin(); MyConstIterator != MyContainer.end(); ++MyConstIterator)
       { 
          Sum += GetElement(*MyConstIterator);
       }
   return Sum; /
}
 
 
 
template<typename Iterator>
double Sum(Iterator myFirstIterator, Iterator mySecondIterator)
{
    double Sum = 0;
    for (; myFirstIterator != mySecondIterator; ++myFirstIterator)
        {
            Sum += GetElement(*myFirstIterator);
        }
    return Sum;
}
 
int main ()
{
 
    const unsigned int size_of_data_array = 35; 
 
        double data_doubles[size_of_data_array] 
                    =  { 1830 , 190.1, 1914. , 1863 , 1839 ,        // 0 - 4
                          } ;       
 
 
    
    
    
        std::map <int, double > My_SumTest_Map;
 
        for (int i=0;i<10;i++) My_SumTest_Map[i] = i*10.5;
 
        std::map<std::string, double > MapStringDouble;
        
        MapStringDouble ["The Wireless"] = data_doubles[1];
        MapStringDouble ["WWI"] = data_doubles[2];
        MapStringDouble ["Alfred Nobel"] = data_doubles[3];
 
        std::map<std::string, double>::const_iterator iMy_Map_Iterator = MapStringDouble.cbegin();
 
        //std::map<int, double>::const_iterator iMy_Map_Iterator = My_SumTest_Map.cbegin();
    
        std::cout << "\n-----Print without accumlating------" << std::endl;
 
        for (iMy_Map_Iterator; iMy_Map_Iterator != MapStringDouble.cend(); ++iMy_Map_Iterator)
                {
                    std::cout   << "\n["    ; iMy_Map_Iterator->first   ;
                    std::cout       << "] -> " 
                                << "("      << iMy_Map_Iterator->second<<")";
                }
 
 
            //std::cout << "\n Sum of all MyMap elements   is  :  " <<  Sum(My_SumTest_Map) ;
        std::cout << "\n Sum of all MyMap elements   is  :  "   <<  Sum(MapStringDouble) ;
            std::cout << "\n\n\n";
return 0;
}


Ребят подскажите в чем дело если делать map <int,double > то все ок если делать std::map<std::string, double> то все как то не радужно... вылезают какие ошибки связанные с const и все такое

вот список ошибок
errors

1>------ Build started: Project: L7_ex2_map_string_problem, Configuration: Debug Win32 ------
1> main.cpp
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\list(1588) : see declaration of 'std::operator <'
1> c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(124) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1> with
1> [
1> _Ty=std::string
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\map(71) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1> with
1> [
1> _Ty=std::string
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(451) : see reference to class template instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=double,
1> _Pr=std::less<std::string>,
1> _Alloc=std::allocator<std:air<const std::string,double>>,
1> _Mfl=false
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(520) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,double,std::less<std::string>,std::allocator<std:air<const std::string,double>>,false>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(659) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,double,std::less<std::string>,std::allocator<std:air<const std::string,double>>,false>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\map(81) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,double,std::less<std::string>,std::allocator<std:air<const std::string,double>>,false>
1> ]
1> c:\all my\с++\lab_for_level_7_8\l7_ex2_map_string_problem\main.cpp(63) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=double
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\list(1588) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\list(1588) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(1885) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(1885) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(1885) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\vector(1502) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\vector(1502) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\vector(1502) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\memory(2582) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\memory(2582) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\memory(2582) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1356) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1356) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1356) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1179) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1179) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(1179) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std:air<_Ty1,_Ty2> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\utility(318) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std:air<_Ty1,_Ty2> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\utility(318) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std:air<_Ty1,_Ty2> &' from 'const std::string'
1> c:\program files\microsoft visual studio 10.0\vc\include\utility(318) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

надо было инклюднуть
#include <string>
После регистрации реклама в сообщениях будет скрыта и будут доступны все возможности форума.
MoreAnswers
Эксперт
37091 / 29110 / 5898
Регистрация: 17.06.2006
Сообщений: 43,301
13.08.2012, 21:45     STL Iterators
Еще ссылки по теме:

Оибка vector iterators incompatible ? C++
vector(выскакивает ошибка "vector iterators incompatible") C++
C++ Map/set iterators are incompatible

Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
DU
1477 / 1053 / 45
Регистрация: 05.12.2011
Сообщений: 2,279
13.08.2012, 21:45     STL Iterators #22
нету #include <string>

до кучи есть смысл переюзать интервальную функцию суммы в функции, которая контейнер принимает:

C++
1
2
3
4
5
template<typename T> 
double Sum(const T& MyContainer)
{
   return Summ(MyContainer.begin(), MyContainer.end());
}
Yandex
Объявления
13.08.2012, 21:45     STL Iterators
Ответ Создать тему
Опции темы

Текущее время: 23:12. Часовой пояс GMT +3.
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Рейтинг@Mail.ru