C++ 如何获取字符串向量中某个元素的位置,将其用作整数向量中的索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15099707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:01:29  来源:igfitidea点击:

How to get position of a certain element in strings vector, to use it as an index in ints vector?

c++

提问by Nour

I am trying to get the index of an element in a vector of strings, to use it as an index in another vector of inttype, is this possible ?

我正在尝试获取向量中元素的索引strings,以将其用作另一个int类型向量中的索引,这可能吗?

Example:

例子:

vector <string> Names;
vector <int> Numbers;

 ... 
// condition to check whether the name exists or not
if((find(Names.begin(), Names.end(), old_name_)) != Names.end())  
    {   // if yes
        cout <<"Enter the new name."<< endl;
        cin >> name;
        replace(Names.begin(), Names.end(), old_name_, name);
    }

Now I want to get the position of old_namein the Namesvector, to use it in accessing certain element in Numbersvector. So that I can say:

现在,我想要得到的位置old_nameNames矢量,在访问某些元素使用它Numbers载体。这样我就可以说:

Numbers[position] = 3 ; // or whatever value assigned here.

I tried using:

我尝试使用:

vector <string> :: const_iterator pos;
pos = (find(Names.begin(), Names.end(), old_name_))
Numbers[pos] = 3;

but obviously this doesn't work since posis of type string !

但显然这不起作用,因为它pos是字符串类型!

回答by dasblinkenlight

To get a position of an element in a vector knowing an iterator pointing to the element, simply subtract v.begin()from the iterator:

要在知道指向元素的迭代器的情况下获取元素在向量中的位置,只需v.begin()从迭代器中减去:

ptrdiff_t pos = find(Names.begin(), Names.end(), old_name_) - Names.begin();

Now you need to check posagainst Names.size()to see if it is out of bounds or not:

现在,你需要检查posNames.size(),看它是否是界限或不出来:

if(pos >= Names.size()) {
    //old_name_ not found
}

vector iterators behave in ways similar to array pointers; most of what you know about pointer arithmetic can be applied to vector iterators as well.

向量迭代器的行为方式类似于数组指针;您对指针算术的了解大部分也可以应用于向量迭代器。

Starting with C++11 you can use std::distancein place of subtraction for both iterators and pointers:

从 C++11 开始,您可以使用std::distance迭代器和指针代替减法:

ptrdiff_t pos = distance(Names.begin(), find(Names.begin(), Names.end(), old_name_));

回答by juanchopanza

If you want an index, you can use std::findin combination with std::distance.

如果需要索引,可以std::findstd::distance.

auto it = std::find(Names.begin(), Names.end(), old_name_);
if (it == Names.end())
{
  // name not in vector
} else
{
  auto index = std::distance(Names.begin(), it);
}

回答by javer

I am a beginner so here is a beginners answer. The if in the for loop gives i which can then be used however needed such as Numbers[i] in another vector. Most is fluff for examples sake, the for/if really says it all.

我是初学者,所以这里是初学者的答案。for 循环中的 if 给出了 i ,然后可以使用它,但需要使用另一个向量中的 Numbers[i] 。例如,大多数都是绒毛,for/if 真的说明了一切。

int main(){
vector<string>names{"Sara", "Harold", "Frank", "Taylor", "Sasha", "Seymore"};
string req_name;
cout<<"Enter search name: "<<'\n';
cin>>req_name;
    for(int i=0; i<=names.size()-1; ++i) {
        if(names[i]==req_name){
            cout<<"The index number for "<<req_name<<" is "<<i<<'\n';
            return 0;
        }
        else if(names[i]!=req_name && i==names.size()-1) {
            cout<<"That name is not an element in this vector"<<'\n';
        } else {
            continue;
        }
    }