2013-10-06 13 views
16

में तत्व पाया गया है, तो मैं कैसे जांच सकता हूं कि मेरे सरणी में कोई तत्व है जिसे मैं ढूंढ रहा हूं?जांचें कि सरणी C++

Foo someObject = new Foo(someParameter); 
Foo foo; 
//search through Foo[] arr 
for(int i = 0; i < arr.length; i++){ 
    if arr[i].equals(someObject) 
    foo = arr[i]; 
} 
if (foo == null) 
    System.out.println("Not found!"); 
else 
    System.out.println("Found!"); 

लेकिन C++ मुझे नहीं लगता कि अगर एक वस्तु अशक्त तो क्या सी ++ समाधान होगा मैं खोज करने के लिए अनुमति दी है:

जावा में, मैं कुछ इस तरह करना होगा?

उत्तर

36

C++ में आप std::find का प्रयोग करेंगे, और, सीमा की समाप्ति के लिए अगर परिणामी सूचक अंक जाँच इस तरह:

Foo array[10]; 
... // Init the array here 
Foo *foo = std::find(std::begin(array), std::end(array), someObject); 
// When the element is not found, std::find returns the end of the range 
if (foo != std::end(array)) { 
    cerr << "Found at position " << std::distance(array, foo) << endl; 
} else { 
    cerr << "Not found" << endl; 
} 
+1

'10' कहां से आ रहा है? –

+1

@JamesMcMahon यह मेरे मन में आकार के आकार के लिए चुने गए मनमानी नंबर है। – dasblinkenlight

+0

चूंकि इसका उपयोग डुप्लिकेट लक्ष्य के रूप में किया जाता है, और सी ++ 11 अब 5 साल का है, 'array + 10' को' std :: end (array) 'के साथ बदलने पर विचार करें? – Yakk

-4

सी ++ शून्य भी है, अक्सर एक ही रूप में 0 (सूचक को संबोधित करने के 0x00000000)।

Do you use NULL or 0 (zero) for pointers in C++?

तो सी में है कि रिक्त जांच ++ होगा:

if (!foo) 
    cout << "not found"; 
+0

जब मैं इसे इस तरह से प्रयास करता हूं, तो यह कहता है कि "ऑपरेटर" मेरे ऑब्जेक्ट के लिए नहीं मिला। –

+0

या आप कोशिश कर सकते हैं (foo == NULL) – canhazbits

3

आप एक ही बात करते हैं होता है, अपना शब्द के लिए खोज करने के लिए सरणी के माध्यम से पाशन। बेशक अगर यह एक क्रमबद्ध सरणी इस बहुत तेजी से होगा, इसलिए कुछ इसी तरह prehaps करना सीखें:

for(int i = 0; i < arraySize; i++){ 
    if(array[i] == itemToFind){ 
     break; 
    } 
} 
6

ऐसे कई तरीके हैं ... एक std::find() एल्गोरिथ्म का उपयोग करने, उदा है

#include <algorithm> 

int myArray[] = { 3, 2, 1, 0, 1, 2, 3 }; 
size_t myArraySize = sizeof(myArray)/sizeof(int); 
int *end = myArray + myArraySize; 
// find the value 0: 
int *result = std::find(myArray, end, 0); 
if (result != end) { 
    // found value at "result" pointer location... 
} 
2

आप नौकरी करने के लिए पुराने सी-शैली प्रोग्रामिंग का उपयोग कर सकते हैं। इसके लिए सी ++ के बारे में थोड़ा ज्ञान की आवश्यकता होगी। शुरुआती के लिए अच्छा है। find, find_if, any_of, for_each, या नए for (auto& v : container) { } वाक्य रचना:

आधुनिक सी ++ भाषा के लिए आप आमतौर पर लैम्ब्डा, समारोह वस्तुओं, ... या एल्गोरिथ्म के माध्यम से यह पूरा। find कक्षा एल्गोरिदम कोड की अधिक पंक्तियां लेता है। आप अपनी विशेष आवश्यकता के लिए अपना खुद का टेम्पलेट find फ़ंक्शन भी लिख सकते हैं।

यहाँ मेरी नमूना कोड

#include <iostream> 
#include <functional> 
#include <algorithm> 
#include <vector> 

using namespace std; 

/** 
* This is old C-like style. It is mostly gong from 
* modern C++ programming. You can still use this 
* since you need to know very little about C++. 
* @param storeSize you have to know the size of store 
* How many elements are in the array. 
* @return the index of the element in the array, 
* if not found return -1 
*/ 
int in_array(const int store[], const int storeSize, const int query) { 
    for (size_t i=0; i<storeSize; ++i) { 
     if (store[i] == query) { 
     return i; 
     } 
    } 
    return -1; 
} 

void testfind() { 
    int iarr[] = { 3, 6, 8, 33, 77, 63, 7, 11 }; 

    // for beginners, it is good to practice a looping method 
    int query = 7; 
    if (in_array(iarr, 8, query) != -1) { 
     cout << query << " is in the array\n"; 
    } 

    // using vector or list, ... any container in C++ 
    vector<int> vecint{ 3, 6, 8, 33, 77, 63, 7, 11 }; 
    auto it=find(vecint.begin(), vecint.end(), query); 
    cout << "using find()\n"; 
    if (it != vecint.end()) { 
     cout << "found " << query << " in the container\n"; 
    } 
    else { 
     cout << "your query: " << query << " is not inside the container\n"; 
    } 

    using namespace std::placeholders; 
    // here the query variable is bound to the `equal_to` function 
    // object (defined in std) 
    cout << "using any_of\n"; 
    if (any_of(vecint.begin(), vecint.end(), bind(equal_to<int>(), _1, query))) { 
     cout << "found " << query << " in the container\n"; 
    } 
    else { 
     cout << "your query: " << query << " is not inside the container\n"; 
    } 

    // using lambda, here I am capturing the query variable 
    // into the lambda function 
    cout << "using any_of with lambda:\n"; 
    if (any_of(vecint.begin(), vecint.end(), 
      [query](int val)->bool{ return val==query; })) { 
     cout << "found " << query << " in the container\n"; 
    } 
    else { 
     cout << "your query: " << query << " is not inside the container\n"; 
    } 
} 

int main(int argc, char* argv[]) { 
    testfind(); 

    return 0; 
} 

इस फाइल 'testalgorithm.cpp' नाम दिया गया है कहो है आप के साथ

g++ -std=c++11 -o testalgorithm testalgorithm.cpp 

आशा इस में मदद मिलेगी यह संकलन करने की जरूरत है। अगर मैंने कोई गलती की है तो कृपया अपडेट करें या जोड़ें।

static inline bool exists(int ints[], int size, int k) // array, array's size, searched value 
{ 
    if (size <= 0)  // check that array size is not null or negative 
     return false; 
    // sort(ints, ints + size); // uncomment this line if array wasn't previously sorted 
    return (std::binary_search(ints, ints + size, k)); 
} 

संपादित करें: के अवर्गीकृत पूर्णांक सरणी अगर uncommenting प्रकार के लिए काम करता

1

आप मूल रूप से this question (int value in sorted (Ascending) int array) का जवाब देख रहे थे, तो आप निम्न कोड कि एक द्विआधारी खोज (सबसे तेजी से परिणाम) करता है का उपयोग कर सकते ।

संबंधित मुद्दे