Thread: locate index

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

    locate index

    It determines the location index of a number k.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int a[1024], k, x, i, flag;
    	unsigned int n;
    
    	cin >> k >> n;
    
    	for ( i = 0; i < n; i++ )
    	{
    		cin >> x;
    		a[i] = x;
    	}
    	for ( i = 0; i < n; i++ )
    	{
    		if ( a[i] == k )
    		{
    			flag = i;
    			break;
    		}
    		else
    			flag = -1;
    	}
    	cout << flag <<endl;
    }
    If I use vector instead of array, is there a function that can do that?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    std::find. To convert the iterator you get to an index, you have to subtract v.begin() from it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    How do I use this function can you give me an example?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    std::vector<int> v;
    fill(v);
    
    std::vector<int>::iterator loc = std::find(v.begin(), v.end(), k);
    std::vector<int>::size_type idx = loc == v.end() ? -1 : loc - v.begin();
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can find a brief description of how to use std::find, along with two examples, at cppreference.com's entry on std::find. Notice that the second example actually demonstrates using std::find with an array.

    If you are using an associative container like std::map, on the other hand, then it would be better to use the find function provided by that container as it would be more efficient.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Nice. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Reiventing the wheel (again)
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2007, 03:26 AM
  3. Getting index of chosen item in the combo box
    By what3v3r in forum Windows Programming
    Replies: 4
    Last Post: 09-01-2006, 11:49 AM
  4. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  5. file index update error
    By daluu in forum C Programming
    Replies: 1
    Last Post: 04-28-2003, 02:47 AM