2017-11-27 13 views
-1

のは、私は以下の目的を持っているとしましょう:私は、データオブジェクト内の最初の非繰り返しエントリを見つけようとしているfind_first_not_ofを文字列のベクトルで使用するには?

vector<string> data = {"12","12","12","12","13","14","15", "15", "15", "15", "18"}; 

。 たとえば、data.find_first_not_of(data.at(0));これは、データが文字列型のみ(コンテナなし)である場合に機能します。

ベクトル型のオブジェクトで同じことをどうすれば実現できますか?

私はアルゴリズムライブラリからadjacent_findとfind_if_notを見ましたが、役に立たなかった。

あなたの提案は大変ありがたいです。

+0

は常にソートされたデータですか? – PaulMcKenzie

+2

あなたのタイトル、または任意の 'ベクトル'、または 'ベクトル 'のように'ベクトル 'を書いていますか?あなたの質問からはっきりと分かりません。 – Useless

+0

残念ながら、そうではありません!私は要素の順序を混乱させることはできません、それはそのままでなければなりません。 – Xigma

答えて

2

adjacent_findで問題がありましたか?あなたは逆述語とそれを使用することができるはずです。

std::vector<std::string> data = {"12","12","12","12","13","14","15", "15", "15", "15", "18"}; 

// Sort data here if necessary 

auto itr = std::adjacent_find(data.cbegin(), data.cend(), std::not_equal_to<std::string>{}); 
if (itr != data.cend()) { 
    std::cout << "First mismatch: " << *itr << " " << *std::next(itr) << std::endl; 
} else { 
    std::cout << "All elements equal" << std::endl; 
} 

Wandbox

+0

これは、OP _did_と一致しますが、必要と主張したものと一致しません。 – Useless

+0

私はnot_equal_toを述語として使用していませんでした。私が持っていたものは、2つのエントリーのためにしか働かない。このサンプルは、このテストサンプル "00"、 "00"、 "00"、 "11"の "all elements equal"を報告します。それは最後の要素をチェックしていません、なぜか? – Xigma

+0

@Xigma私にとってうまくいく:https://wandbox.org/permlink/pYt127SGWU9vH9Qe – 0x5453

1

あなたは少なくとも一度リストを通過する必要があり、あなたが知っていない場合、またはどこで重複が発生しますので、 (ある場合)、これを解決する1つの方法は、最初に "統計"を収集し、次に収集したものから最初の非重複を判断することです。

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

// struct to hold some information on the numbers 
struct info 
{ 
    std::string number; 
    int count; 
    int position; 
    info(const std::string n, int c, int p) : number(n), count(c), position(p) {} 
}; 

int main() 
{ 

    std::vector<std::string> data = {"12","12","12","12","13","14","15", "15", "15", "15", "18"}; 
    std::unordered_map<std::string, info> infoMap; 
    std::vector<info> vInfo; 
    int pos = 0; 

    // loop for each data element 
    std::for_each(data.begin(), data.end(), [&](const std::string& n) 
    { 
     // insert entry into the map 
     auto pr = infoMap.insert(std::make_pair(n, info(n, 0, pos)));  

     // bump up the count for this entry. 
     ++pr.first->second.count; 

     // bump up the postion number 
     ++pos; 

    }); 

    // create a vector of the information with a count of 1 item. 
    std::for_each(infoMap.begin(), infoMap.end(), [&](std::unordered_map<std::string, info>::value_type& vt) { if (vt.second.count == 1) vInfo.push_back(vt.second); }); 

    // sort this by position 
    std::sort(vInfo.begin(), vInfo.end(), [&](const info& pr1, const info &pr2){return pr1.position < pr2.position; }); 

    // output the results 
    if (vInfo.empty()) 
     std::cout << "All values are duplicated\n"; 
    else 
     std::cout << "The first number that isn't repeated is " << vInfo.front().number << "\n"; 
    } 

Live Example

まず、我々は単にベクトル内のすべてのエントリを通過し、ちょうど各項目のカウントを集計:

はここstd::unordered_mapを使った例です。さらに、アイテムが見つかった元のリストにその位置を格納します。

その後、カウント数が1のものを除外してベクトルにコピーします。次に、元のリストで見つかった位置に基づいてこのベクトルをソートします。

+0

ありがとうございます。しかし、私はそれが私が達成したいと思うものには残酷だと信じています。 – Xigma

関連する問題