  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 # Fastest way to find pair in a vector, remove it while iterating ## Fastest way to find pair in a vector, remove it while iterating Tag : cpp , By : Ari Date : November 29 2020, 04:01 AM

this one helps. It is really hard to say what the "fastest way" to do anything is. Even "fast enough" is problematic without knowing your constraints exactly. Therefore, I'm going to give you a few tips to get a (probably) "faster" program; whether it will be fast enough is up to you to decide.
First of all, you probably want to change your sort criterion. Instead of sorting on the second component (the end of the interval, I assume,) you need to sort on the first component and then on the second. This way, an interval that starts sooner will be earlier in the array, and among intervals with the same start, the one that is shortest will be first.
``````displacement = 0
for all elements in the array, do:
if current element should be removed, then:
increment "displacement"
else:
move current element back "displacement" places

delete last "displacement" elements
``````
``````let original vector be A, and the new vector of pairs be B,
t0 = -1
t1 = -1
for all elements in A, do:
if start time of current element is greater than "t1", then:
append the pair (t0, t1) to B
t0 = start time of current element
t1 = end time of current element
else:
t1 = max(t1, end time of current element)
append the pair (t0, t1) to B
remove first element of B (because it is (-1, -1))
`````` Boards Message : You Must Login Or Sign Up to Add Your Comments .

## Clojure: Iterating over a vector of vectors to find the first vector that meets a certain condition

Tag : development , By : yossi
Date : March 29 2020, 07:55 AM
around this issue To learn Clojure I'm working on a little Tic Tac Toe game. After completing the first part of the game with relative ease, I've struggled trying to build an intelligent computer player. , First of all, I advice you to use this structure for board position:
``````(def board [[1 1 0]
[0 0 0]
[1 0 1]])
``````
``````(def winning-sets
'([[0 0] [0 1] [0 2]]
[[1 0] [1 1] [1 2]]
[[2 0] [2 1] [2 2]]
[[0 0] [1 0] [2 0]]
[[0 1] [1 1] [2 1]]
[[0 2] [1 2] [2 2]]
[[0 0] [1 1] [2 2]]
[[0 2] [1 1] [2 0]]))
``````
``````(defn check
[target combo]
(= (map #(count (filter (partial = %) combo)) [target 0]) '(2 1)))

(defn extract
[coords]
(apply vector (map (fn [[f s]] ((board f) s)) coords)))

(filter #(check 1 (extract %)) winning-sets)
``````
``````user=> (filter #(check 1 (extract %)) winning-sets)
([[0 0] [0 1] [0 2]]
[[2 0] [2 1] [2 2]]
[[0 0] [1 0] [2 0]]
[[0 0] [1 1] [2 2]])
``````

## Iterating in a sorted manner over a std::vector<std::pair<T,U> > object

Tag : cpp , By : yatil
Date : March 29 2020, 07:55 AM
Hope this helps I am reading a object from a database of type Foo, as defined below. This object is a vector of Foo Members, where a Foo Members consists of a string id and a container object. , You can use std::sort
``````#include <algorithm>

bool comparator(const FooMember& i, const FooMember& j)
{
std::string str1 = i.first;
boost::algorithm::to_lower(str1);
std::string str2 = j.first;
boost::algorithm::to_lower(str2);
return (str1 < str2);
}

void sortFoo(Foo& value) {
std::sort (value.begin(), value.end(), comparator);
}
``````

## Fastest way to find all-pair shortest paths in a undirected cycle

Tag : algorithm , By : demize95
Date : March 29 2020, 07:55 AM
To fix this issue In one pass starting from A traverse the graph clockwise, and for every node compute the distance from A. Let's say the distance to the node X is a[X]. This way for any pair (X, Y) of nodes the distance will be:
``````min(abs(aX - aY), total - abs(aY - aX))
``````
``````min(abs(aB - aF), total - abs(aB - aF)) =
min(abs( 1 -  3),     6 - abs( 1 -  3)) =
min(           2,                    4) =
2
``````

## Fastest way to find a vector in a vector of vectors

Tag : cpp , By : S. Fenz
Date : March 29 2020, 07:55 AM

## Find int in Vector of pairs vector <pair <int, vector <SavingsAccount*>>> using find_if

Tag : cpp , By : Jonathan Bernard
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():
``````#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());
}
`````` 