Sabtu, 01 Agustus 2026

C++ search in array

 

Conversation wit

#include <iostream>
#include <string>
#include <algorithm> // Required for std::for_each and std::find
#include <iterator>  // Required for std::begin and std::end

int main() {
    // 1. Array of 5 names
    std::string names[5] = {"Alice", "Bob", "Charlie", "Diana", "Ethan"};

    // 2. View all members of the array (using std::for_each from <algorithm>)
    std::cout << "Names in the array:\n";
    std::for_each(std::begin(names), std::end(names), [](const std::string& name) {
        std::cout << "- " << name << "\n";
    });
    std::cout << "\n";

    // 3. Get input from user using std::cin
    std::string targetName;
    std::cout << "Enter a name to search for: ";
    std::cin >> targetName;

    // 4. Search using std::find from <algorithm>
    auto result = std::find(std::begin(names), std::end(names), targetName);

    // 5. Output the result
    if (result != std::end(names)) {
        int index = std::distance(std::begin(names), result);
        std::cout << "Success! '" << targetName << "' was found at index " << index << ".\n";
    } else {
        std::cout << "Sorry, '" << targetName << "' was not found in the array.\n";
    }

    return 0;
}

C++ search with cin

 

Conversation wit

Here is a complete C++ code sample that initializes an array of 5 names, takes input using std::cin >>, and searches for the name using std::find from the <algorithm> header:




C++
#include <iostream>
#include <string>
#include <algorithm> // Required for std::find
#include <iterator>  // Required for std::begin and std::end

int main() {
    // 1. Array of 5 names
    std::string names[5] = {"Alice", "Bob", "Charlie", "Diana", "Ethan"};

    // 2. Get input from user using std::cin
    std::string targetName;
    std::cout << "Enter a name to search for: ";
    std::cin >> targetName;

    // 3. Search using std::find from <algorithm>
    // std::find returns an iterator pointing to the found element, or std::end if not found.
    auto result = std::find(std::begin(names), std::end(names), targetName);

    // 4. Output the result
    if (result != std::end(names)) {
        // Find the index by calculating distance from the beginning
        int index = std::distance(std::begin(names), result);
        std::cout << "Success! '" << targetName << "' was found at index " << index << ".\n";
    } else {
        std::cout << "Sorry, '" << targetName << "' was not found in the array.\n";
    }

    return 0;
}

C++ search

 #include <iostream>

#include <string>
#include <array>
#include <algorithm>

int main() {
    // 1. Declare and initialize an array of 5 names
    std::array<std::string, 5> names = {"Alice", "Bob", "Charlie", "Diana", "Ethan"};

    // 2. Specify the name to search for
    std::string target = "Charlie";

    // 3. Search using std::find from <algorithm>
    auto it = std::find(names.begin(), names.end(), target);

    // 4. Check if the name was found
    if (it != names.end()) {
        // std::distance calculates the index from the iterator
        int index = std::distance(names.begin(), it);
        std::cout << "Found '" << target << "' at index " << index << "!\n";
    } else {
        std::cout << "'" << target << "' was not found in the array.\n";
    }

    return 0;
}

Key Takeaways

  • std::find(begin, end, value): Iterates through the collection and returns an iterator pointing to the first matching element. If no match is found, it returns names.end().

  • std::array: Preferred over raw C-style arrays (std::string names[5]) because it works seamlessly with C++ standard algorithms like std::find.

  • std::distance: Converts the resulting iterator into a standard 0-based array index.




C++ algorithm

 #include <iostream>

#include <vector>

#include <algorithm> // For std::sort


int main() {

    // 1. Creation and Initialization

    std::vector<int> numbers = {5, 2, 8, 1, 9};


    // 2. Adding elements

    numbers.push_back(4); // Adds 4 to the end

    numbers.emplace_back(3); // Constructs 3 at the end (often more efficient)


    // 3. Accessing elements

    std::cout << "First element: " << numbers.front() << "\n";

    std::cout << "Element at index 2: " << numbers[2] << "\n";

    std::cout << "Element at index 3 (bound-checked): " << numbers.at(3) << "\n";

    std::cout << "Last element: " << numbers.back() << "\n";


    // 4. Modifying and Sorting

    std::sort(numbers.begin(), numbers.end()); // Sorts the vector


    // 5. Removing elements

    numbers.pop_back(); // Removes the last element (3)


    // 6. Iterating through the vector

    std::cout << "\nSorted vector elements: ";

    for (int num : numbers) {

        std::cout << num << " ";

    }

    std::cout << "\n";


    // 7. Size and Capacity

    std::cout << "Current size: " << numbers.size() << "\n";

    std::cout << "Is vector empty? " << (numbers.empty() ? "Yes" : "No") << "\n";


    // 8. Clearing the vector

    numbers.clear();

    std::cout << "Size after clear: " << numbers.size() << "\n";


std::cout<<"======================"<<std::endl;


    // 1. Initialize a vector with 5 names

    std::vector<std::string> names = {"Charlie", "Alice", "Eve", "Bob", "Diana"};


    // 2. Sort the names in ascending (alphabetical) order

    std::sort(names.begin(), names.end());


    // 3. Output the sorted names

    std::cout << "Sorted names:\n";

    for (const auto& name : names) {

        std::cout << name << "\n";

    }



    return 0;

}