  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Trying to use find_if function to locate value in vector of pairs by first element  » cpp » Trying to use find_if function to locate value in vector of pairs by first element

By : habourret
Date : October 23 2020, 06:10 AM
wish helps you The problem is there is no built-in comparison between a pair and what appears to be a string. You will have to provide one. Eg. code :
``````it = find_if (vect.begin(),
vect.end(),
[currentword](const pair <string,int>& p){ // this is a lambda expression
return p.first == currentword; // compare strings
});
``````
``````std::map<string, int> freqmap;
``````
``````freqmap[currentword]++;
`````` ## Find_if function in vector

By : Abubaker Sarang
Date : March 29 2020, 07:55 AM
Any of those help Find_if is an function of an and returns an iterator to the first element in the range.
code :
``````   #include <list>
#include <algorithm>
#include <iostream>

bool greater10 ( int value )
{
if(value == 10)
{
return true;
}
return false;
}

int main( )
{
using namespace std;

list <int> L;
list <int>::iterator Iter;
list <int>::iterator result;

L.push_back( 10 );
L.push_back( 9 );
L.push_back( 15 );
L.push_back( 8);
L.push_back( 10 );

cout << "L = ( " ;
for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
cout << *Iter << " ";
cout << ")" << endl;

result = find_if( L.begin( ), L.end( ), &greater10 );
for( ;result != L.end(); result++ )
{
cout << *result <<"\t";
cout <<endl;
}
}
`````` ## std::find_if for custom objects returns bad iterator when vector has at least 1 element

By : user3254095
Date : March 29 2020, 07:55 AM
this one helps. The code you wrote isn't portable because it uses implementation-specific _Ptr member of vector::iterator, and therefore there are no requirements on that member to be nullptr. Change
code :
``````if (player._Ptr != nullptr)
``````
``````if (player != m_playerList.end())
`````` ## Access an element in a vector of pairs by a key besides find_if() or iterators

By : user1179004
Date : March 29 2020, 07:55 AM
Hope this helps
I'm trying to access an element by using the keys like an index. Unfortunately, I always receive a segmentation fault.
code :
``````MyWords[key1].second[key2].second.count = A_Number_Here;
``````
``````#include <iostream>
#include <vector>
#include <algorithm>

struct Item
{
std::string word;
std::vector<int> count; // changed to vector array
};
using Pair = std::pair<int, std::vector< std::pair<int, Item> > >;

int main()
{
std::vector< Pair > MyWords =
{  //int, <std::pair<int,          Item            > > >
{1   , {        {    4,  Item{"String1", {1,2,3}} } }       },
{0   , {        {    5,  Item{"String2", {5,2,8}} } }       },
{2   , {        {    8,  Item{"String3", {1,7,9}} }, {    9,  Item{"String4", {11,77,99}} } }       }
};

for(const auto& bigPair: MyWords)
{
std::cout << "Key : " << bigPair.first;

for(const auto& smallPair: bigPair.second)
{
std::cout << "\nValues: " << smallPair.first << "\t";

std::cout << smallPair.second.word << " "
<< smallPair.second.count << " "
<< smallPair.second.count << " "
<< smallPair.second.count ;
}
std::cout << "\n\n";
}

return 0;
}
`````` ## Using std::find_if with std::vector to find smallest element greater than some value

By : Scott
Date : March 29 2020, 07:55 AM
it fixes the issue 1 line of code is necessary, if a lambda function is used: x is a local double (value we are searching for) const double x = 5.5
code :
``````std::find_if(vec.cbegin(), vec.cend(), [x] (double element) { return (x < element); })
``````
``````std::find_if(vec.cbegin(), vec.cend(), lambda)
``````
``````[x] (double element) { return (x < element); }
`````` ## Find int in Vector of pairs vector <pair <int, vector <SavingsAccount*>>> using find_if

By : Omer Alhalabi
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further My data structure is: , Use a lambda as predicate for std::find_if():
code :
``````#include <algorithm>
#include <utility>
#include <vector>
#include <iostream>

using namespace std;

struct SavingsAccount{};

int main()
{
vector<pair<int, vector<SavingsAccount>>> accVec;
vector<pair<int, vector<SavingsAccount>>>::iterator iter;

do {
cout << "Enter The Account Number In Which You Want To Deposit: ";
int accID;
cin >> accID;

iter = find_if(accVec.begin(), accVec.end(),
[=](pair<int, vector<SavingsAccount>> const &accPair) {
return accPair.first == accID;
}
);

if (iter == accVec.end()) {
cout << endl << "ERROR: Account Does Not Exist. Try Again." << endl;
}
} while (iter == accVec.end());
}
``````
``````#include <algorithm>
#include <utility>
#include <vector>
#include <iostream>

using namespace std;

struct SavingsAccount{};

class AccountFinder
{
int accID;

public:
AccountFinder(int accID) : accID{ accID } {};

bool operator()(pair<int, vector<SavingsAccount>> const &accPair)
{
return accPair.first == accID;
}
};

int main()
{
vector<pair<int, vector<SavingsAccount>>> accVec;
vector<pair<int, vector<SavingsAccount>>>::iterator iter;

do {
cout << "Enter The Account Number In Which You Want To Deposit: ";
int accID;
cin >> accID;

iter = find_if(accVec.begin(), accVec.end(), AccountFinder(accID));

if (iter == accVec.end()) {
cout << endl << "ERROR: Account Does Not Exist. Try Again." << endl;
}
} while (iter == accVec.end());
}
`````` 