“Temukan Elemen di Vektor” Kode Jawaban

C Pencarian Elemen Vektor

#include <algorithm>
#include <vector>
vector<int> vec; 
//can have other data types instead of int but must same datatype as item 
std::find(vec.begin(), vec.end(), item) != vec.end()
Embarrassed Eland

C Temukan elemen di vektor

auto it = find(vec.begin(),vec,end(), item)!
if(it != vec.end()){
 	 int index = it - vec.begin();
}
Troubled Tamarin

vector.find ()

#include <algorithm>
#include <vector>

if ( std::find(vec.begin(), vec.end(), item) != vec.end() )
   do_this();
else
   do_that();
Cooperative Capuchin

Pencarian vektor dengan elemen

#include <vector> // vector 
#include <algorithm> // find 
#include <iostream> // cout 
using namespace std;
int main()
{
    vector<int> nums = {1,2,3,4,5,6,7,8,9};
    bool isSorted = is_sorted(nums.begin(), nums.end());
    if(isSorted){
        cout << "Using binary search: " << endl;
        if(binary_search(nums.begin(), nums.end(), 9))
            cout << "found it" << endl;
        else
            cout << "not here" << endl;
    }
    else{
        cout << "Using std::find";
        if(std::find(nums.begin(), nums.end(), 9) != nums.end())
            cout << "found it" << endl;
        else
            cout << "not here" << endl;
    }
}
intricate_symbol

Temukan Elemen di Vektor


    it = find (vec.begin(), vec.end(), ser);
    if (it != vec.end())
    {
        cout << "Element " << ser <<" found at position : " ;
        cout << it - vec.begin() << " (counting from zero) \n" ;
    }
    else{
        cout << "Element not found.\n\n";
    }
it's_flat

Jawaban yang mirip dengan “Temukan Elemen di Vektor”

Pertanyaan yang mirip dengan “Temukan Elemen di Vektor”

Lebih banyak jawaban terkait untuk “Temukan Elemen di Vektor” di C++

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya