2017-04-07 40 views
-1

讓我們假設有以下對檢查如果給定的載體中已經包含在C對元件++

0 -1 
0 -2 
0 -3 
1- 2 
2 -3 
3- 4 
2 -4 
4- 5 
5 -6 

我想那些對插入到載體中,從而使我應該有各元素中只有一個次,例如 空載體入手:

0-1插入 現在我們正在檢查0-2,存在0,而不是2,所以0-2插入,我們有

0-1 
0-2 

現在0-3,3是不在列表中,這樣我們就可以插入

0-1 
0-2 
0-3 

現在讓我們來考慮1-2個,我們當然有他們兩個,所以跳過,現在讓我們考慮的2- 3,再次我們可以跳過,3-4,3存在但不是4,所以我們可以插入 3-4,在插入4之後,4也存在,所以拒絕2-4然後出現4-5和5-6,所以我們下面的列表

0-1 
0-2 
0-3 
3-4 
4-5 
5-6 

我有以下代碼

#include<iostream> 
#include<vector> 
#include<set> 
#include<algorithm> 
using namespace std; 
struct edge 
{ 
    int a, c; 
    float weight;//edge a-c has weight 
    bool operator() (edge x, edge y) 
    { 
     x.weight < y.weight; 

    } 
}; 
int noncyclical_sum(vector<edge>s) 
{ 
    int total = 0; 
    std::vector<std::pair<int, int>> b; 
    auto m = make_pair(s[0].a, s[0].c); 
    b.push_back(m); 
    total = total + s[0].weight; 
    vector<edge>::iterator it; 
    for (int i = 1; i < s.size(); i++) 
    { 
     auto m = make_pair(s[i].a, s[i].c); 
     //if (find(b.begin(), b.end(), s[i].a) != b.end() && find(b.begin(), b.end(), s[i].c) != b.end()) 

      if (find(b.begin(), b.end(), m.first) != b.end() && find(b.begin(), b.end(), m.second) != b.end()) 
      { 
      continue; //both element is in the vector 
     } 
       else 
     { 

      b.push_back(m); 
      total = total + s[i].weight; 
        } 

     std::vector<std::pair<int, int>>::iterator ii; 
     for (ii = b.begin(); ii != b.end(); ii++) 
      cout << ii->first << " " << ii->second; 

      } 

} 
int main() 
{ 



    return 0; 
} 

第一次,我推了第一對,從第二個開始,我檢查是否在同一時間兩個元素是矢量,我拒絕對和繼續,否則我推新對繼續,但我有以下錯誤

Severity Code Description Project File Line Suppression State 
Error C2678 binary '==': no operator found which takes a left-hand operand of type 'std::pair<int,int>' (or there is no acceptable conversion) kurskal_algorithm c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\xutility 3161  

有什麼問題?在此先感謝

+1

你的'布爾運算符()'被竊聽:沒有返回值。 – alexeykuzmin0

+0

不是這一個,這不是問題,我認爲 –

+0

我沒有看到你,包括''其中'std :: pair'(與所有的比較運算符)被定義。 –

回答

3

的問題是在這條線:

if (find(b.begin(), b.end(), m.first) != b.end() && find(b.begin(), b.end(), m.second) != b.end()) 

讓我們來看看std::find調用的參數:b.begin()b.end()std::vector<std::pair<int, int>>::iterator s,而m.firstint

因此,您試圖在pairvector中找到int。你不能那樣做。

此外,您的所有功能缺少所需的return陳述。

+0

我該如何解決這個問題?在此先感謝 –

+0

我已經添加了返回語句的兩個函數,只有一個錯誤 –

+0

@datodatuashvili你應該改變你存儲數據的方式。要在所有「int」的集合中查找「int」,您需要一個存儲它們的「std :: vector 」。 – alexeykuzmin0

相關問題