Assistance to implement the unique() in coding of the C++
Hello, I am the student of the Bsc-CS. I have to submit the Project on the functions of the C++ language in three days. I can have the well knowledge of the functions that can be used in the C++ language. Even if, I can not know much more about the unique() function of the C++ language. So, I want to know about the unique() function of the C++ language. I also want assistance for to implement the unique() function in the C++ Coding. Reply Me!!
The unique() in coding of the C++
Hello, According to me the unique() function can be used in the C++ language to remove the consecutive duplications that can be available in the range. The behavior of the unique() function template can be similar to the following as :
Code:
template <class FrwrdItrtr>
FrwrdItrtr unique ( FrwrdItrtr fst, FrwrdItrtr lst )
{
FrwrdItrtr rslt=fst;
while (++fst != lst)
{
if (!(*rslt == *fst))
*(++rslt)=*fst;
}
return ++rslt;
}
Implement the unique() in coding of the C++
I suggest you to go through the following program step by step to understand easily and that can helpful for you to gather the knowledge of the unique() function in the C++ coding :
Code:
#include <iostream>
#include <algoritrhm>
#include <vector>
using namespace std;
bool mfnctn (int i, int j)
{
return (i==j);
}
int main ()
{
int mit[] = {10,20,20,20,30,30,20,20,10};
vector<int> mvctor (mit,mit+9);
vector<int>::itrerator itr;
itr = unique (mvctor.begin(), mvctor.end());
mvctor.resize( itr - mvctor.begin() );
unique (mvctor.begin(), mvctor.end(), mfnctn);
cout << "mvctor contains:";
for (itr=mvctor.begin(); itr!=mvctor.end(); ++itr)
cout << " " << *itr;
cout << endl;
return 0;
}
Output:
Code:
mvctor contains: 10 20 30 20 10
The unique() in coding of the C++
The following can be the parameters of the unique function in the C++ language :
1. prd : This parameter can be used in the C++ language for the binary predicate that can taking two elements as argument and can returns the result of the comparison between both of them.
2. fst, lst : these parameters can be used in the C++ language for the forward iterators to the final and starting positions of the given specified sequence. The range used can be as[fst,lst).
Re: Assistance to implement the unique() in coding of the C++
The unique() function can be used to remove the consecutive duplicates in the range. If the binary predicate variable can be given, then it can be used to check the two elements to determine whether they can be duplicated or not. The unique() function can returns an iterator to the end of the range that can be modified. The unique() function can runs in the linear time. I guess this information can works for you, if not then reply me.