IpPairVec matches;

blah blah other definitions
 for (unsigned int i = 0; i < matches.size(); ++i)
  {
    drawPoint(img1,matches[i].first);
    drawPoint(img2,matches[i].second);
  
    const int & w = img1->width;
    //.........etc etc etc
  }
  std::cout<< "Matches: " << matches.size();
//This returns a number after  every iteration which is continuous.
//I am able to redirect this to a file or display on console till now.
// I need help in storing the numbers  returned after say 10 or 15 iterations in another vector or array (as the value of matches.size() keeps changing after every iteration of the program.And if the sum of the numbers stored exceed a certain value then do something or else do something else hehehe 


  // finding the sum of matches 30-08-2011

int sum ;
  //Sum the std::vector

if (sum > 5 ) 
           {
  
	
                    nameFile = fopen("rf.txt", "a");
		   fprintf(nameFile, "%s_%i\n",personalNum.c_str(),sum);
		fclose(nameFile);

            }
               else
           {
	
	string unknownFaces ;
	char dstr[256];
	 nameFile = fopen("UnknownPerson.txt", "a");
	 fprintf(nameFile, "%s_\n","UnknownPerson");
	fclose(nameFile);
	ifstream intrudNo ( "intruindex.txt", std::ios::in);

Please help me with storing the value of the matches vector and summing it .I have tried std::accumulate which did not work or redirecting to a text file then reading back from that which is not an elegant way to do that .
A couple of lines of appropriate code will be much much appreciated and save me a lot of time and effort as i am a beginner in C++ and have jumped into the deep end of the pool by trying to learn/implement open CV.

Recommended Answers

All 4 Replies

It looks like you have a vector of pairs, which is slightly harder to use with std::accumulate() because you need a custom function or function object to handle the summation:

#include <algorithm>
#include <iostream>
#include <utility>
#include <string>
#include <vector>

struct sum_second {
    template <typename T, typename U>
    U operator()(U sum, std::pair<T, U> next)
    {
        return sum + next.second;
    }
};

int main()
{
    std::vector<std::pair<std::string, int> > v;
    
    v.push_back(std::make_pair("a", 1));
    v.push_back(std::make_pair("b", 2));
    v.push_back(std::make_pair("c", 3));
    v.push_back(std::make_pair("d", 4));
    
    int sum = std::accumulate(v.begin(), v.end(), 0, sum_second());
    
    std::cout << "Sum: " << sum << '\n';
}

FYI, C++0x simplifies this with an anonymous function by bringing the definition of sum_second and std::accumulate together:

#include <algorithm>
#include <iostream>
#include <utility>
#include <string>
#include <vector>

int main()
{
    std::vector<std::pair<std::string, int>> v = {
        std::make_pair("a", 1),
        std::make_pair("b", 2),
        std::make_pair("c", 3),
        std::make_pair("d", 4),
    };
    
    int sum = std::accumulate(v.begin(), v.end(), 0, 
        [](int sum, std::pair<std::string, int> next) { 
            return sum + next.second; 
        });
    
    std::cout << "Sum: " << sum << '\n';
}
#include <vector>
#include <utility>

int main()
{
    // C++0X
    std::vector< std::pair<int,int> > sequence = { {1,2}, {3,4}, {5,6}, {7,8} } ;
    int sum2nd = 0 ;
    for( const auto& pair : sequence ) sum2nd += pair.second ;
}

Thanks a lot guys ,The code by Narue was extremely elegant,it was a pleasure going through it thanks once again.
P.S. i needed to include <numeric> also

P.S. i needed to include <numeric> also

Indeed. I need to check my warning level.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.