2017-04-08 59 views
1

enter image description here为什么我找不到集合论的正确输出?

我想打印使用数组或元素的向量和成员资格的集的基数。

我能够找到基数,但在同一个程序中找不到成员资格。

这里是我的代码..

#include<iostream> 
#include<vector> 
using namespace std; 

int main(){ 

    vector<int> v; 
    int ch; 
    int j; 

    for(int i=1; cin>>j; i++){ 
     v.push_back(j); 
    } 

    cout << v.size(); 

    cout << " enter element you want to find whether it is a memebr or not: "; 
    cin >> ch; 

    for(int i=0; i < v.size(); i++){ 
     if(v[i] == ch){ 
      cout << "element found"; 
      break; 
     } 
    } 
    return 0; 
} 

回答

1

看看你是在运行过程中取得输入一个循环中,太多的条件验证place.Since终止本你给一个char。它引发了一个异常。所以你的程序没有按预期工作。永远不要那样做。总是询问大小,然后迭代循环多次。这里是一个正在工作的代码:

#include<iostream> 
#include<vector> 
using namespace std; 

int main(){ 

vector<int> v; 
int ch; 
int j,temp; 
int size; 
cin>>size; 
for(int i=1; i<=size; i++){ 
    cin>>temp; 
    v.push_back(temp); 
} 


cout << " enter element you want to find whether it is a memebr or not: "; 
cin >> ch; 



for(int i=0; i < v.size(); i++){ 
    if(v[i] == ch){ 
    cout << "element found"; 
    break; 
    } 

    } 
cout<<v.size(); 

    return 0; 

    } 

希望这会帮助你:)

+0

@bharat ......然后就是找原因“CARDINALITY”如果我们已经知道我们的容器的大小? – Paliwal

+0

在你的情况下,容器的基数和大小是相同的,因为我们没有检查向矢量添加项目的任何条件。所以基数是一样的。如果我们正在检查一些条件,例如只有当整数是偶数时才加入向量,那么基数会改变。然后我们每次添加都要使用count变量来获得基数:) – bharath

0

试试这个:

#include <iostream> 
using namespace std; 
#include<vector> 
#include<stdlib.h> 


int main(){ 

vector<int> v; 
int ch; 
int j; 

cout<<"Enter the size of set:"<<endl; 
int n; 
cin>>n; 

for(int i=0; i<n ; i++){ 
    cin>>j; 
    v.push_back(j); 
} 



cout << v.size()<<endl; 

cout << " enter element you want to find whether it is a memebr or not: "; 
cin >> ch; 



for(int i=0; i < v.size(); i++){ 
    if(v[i] == ch){ 
    cout <<endl<< "element found"; 
    exit(0); 
    } 
    } 
cout<<endl<<"Element not found."; 

    return 0; 

    } 
相关问题