How do I find an element position in std::vector?

前端 未结 10 1873
情深已故
情深已故 2021-01-31 08:40

I need to find an element position in an std::vector to use it for referencing an element in another vector:

int find( const vector& whe         


        
10条回答
  •  失恋的感觉
    2021-01-31 08:48

    Take a vector of integer and a key (that we find in vector )....Now we are traversing the vector until found the key value or last index(otherwise).....If we found key then print the position , otherwise print "-1".

         #include   
         using namespace std;  
    
         int main()  
          {  
              vectorstr;  
              int flag,temp key, ,len,num;
    
               flag=0;
               cin>>len;
    
               for(int i=1; i<=len; i++)  
               {
                   cin>>key;
                   v.push_back(key);
               }
    
               cin>>num;
    
               for(int i=1; i<=len; i++)  
               {  
                 if(str[i]==num)  
                 {  
                    flag++;  
                    temp=i-1;
                    break;
                 }  
               }
    
             if(flag!=0)    cout<

提交回复
热议问题