2013-03-10 22 views
1

我需要做的,如果條件與∉(不屬於)c + +屬條件

條件:

if tj ∉ A(fi) where A(fi) contains some elements 

實施例:

A(f1)= t1, t2 A(f2)= t1, t3 

所以如果T3∉A(F1 ) 做一點事。

我的問題是我不知道該怎麼辦了∉在C++

編輯:

謝謝你的答案。

我有一個更多的問題,但我不知道這是否真的可能,或者如果我的夢想太多。我需要多次運行程序,但每次A(fi)中的元素都會改變。

我知道這是可以做到隨機與0和1所以我在想,而不是放T1它將被0或1,但我不知道怎麼寫的訂單後更換。

像我有A(F1)= {1,0,1,0}和(F2)= {0,1,1,0}因此T1出現在A(F1),T2出現在A( f2),t3出現在A(f1)和A(f2)等中。

對於設定功能它寫道:

「集是存儲以下特定順序的獨特元素的容器。」因此,有可能給一個名字順序

所以我的問題是你認爲是可能的,我怎麼能連接T1,T2,T3 ...?

我找到另外一個問題是,我可能會創造更多的設置有2個以上或A(FI),數量不是提前。我不知道如何在不同時間創建不同數量的集合。可能嗎?

+2

歡迎使用堆棧溢出。讓我們試着縮小你的問題。你的設計中有「t1,t2,t3,f1,f2和A()是什麼類型?」 – 2013-03-10 03:41:00

+3

我們有'std :: includes'。 – chris 2013-03-10 03:45:28

+0

@chris這是非常整潔,我添加了一個例子使用'std :: includes'到我的答案,看看它是如何比較。 – 2013-03-10 03:59:48

回答

0

使用Set容器來存儲一組元素。然後使用方法count並檢查其返回值爲零。

0

假設A是返回std::set一個函數(或到std::set的引用):

auto& s = A(f1); 
if(s.find(t3) == s.end()) 
    std::cout << "Not in set\n"; 
3

可以使用std::set此,這裏是一個例子:

#include <iostream> 
#include <set> 
#include <string> 

int main() 
{ 
    std::set<std::string> myset; 

    myset.insert("t1") ; 
    myset.insert("t2") ; 

    if(myset.count("t2") != 0) 
    { 
    std::cout << "Set contains t2" << std::endl; 
    } 

    if(myset.count("t3") != 0) 
    { 
    std::cout << "Set contains t3" << std::endl; 
    } 

    return 0; 
} 

以及使用std::includes的示例:

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

int main() 
{ 
    std::vector<std::string> v1 {"t1", "t2", "t3", "t4" }; 
    std::vector<std::string> v2 {"t2", "t4"}; 
    std::vector<std::string> v3 {"t3"}; 

    std::cout << "v1 contains the follows elments of v2:" << std::endl ; 
    for (auto i : v2){ 
    std::cout << i << ' '; 
    std::cout << ": " << std::includes(v1.begin(), v1.end(), v2.begin(), v2.end()) << std::endl ; 
    } 

    std::cout << "v1 contains the follows elments of v3:" << std::endl ; 
    for (auto i : v3){ 
    std::cout << i << ' '; 
    std::cout << ": " << std::includes(v1.begin(), v1.end(), v2.begin(), v2.end()) << std::endl ; 
    } 

    return 0; 
}