简体   繁体   English

索引向量C ++

[英]Indexing a Vector C++

I am new to Vectors, I think I have a syntactical error, or maybe I'm not calling .at() properly 我是Vector的新手,我认为我有语法错误,或者我可能没有正确调用.at()

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <vector>

using namespace std;

std::vector<string> symbols;
std::vector<int> symbolPos;

void addSymbol(string entry, int position){
    for (std::vector<string>::const_iterator i = symbols.begin(); i != symbols.end(); ++i){
        cout << "*i is: " << *i << endl;
// vvvvvvvvvvvvvvvvvvvvv Problematic line here vvvvvvvvvvvvvvvvvvvvv        
    if (symbols.at(i)==entry){
// ^^^^^^^^^^^^^^^^^^^^^ Problematic line here ^^^^^^^^^^^^^^^^^^^^^
            cout << "same entry" << endl;
            break;
        }

        else{
            symbols.push_back(entry);
            symbolPos.push_back(position);
        }
    }
}

My compiler is throwing an error, saying that .at() cannot be found. 我的编译器抛出错误,说找不到.at()。 What am I doing wrong here? 我在这里做错了什么?

assembler.cpp: In function ‘void addSymbol(std::string, int)’:
assembler.cpp:31: error: no matching function for call to ‘std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::at(__gnu_cxx::__normal_iterator<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)’
/usr/lib/gcc/i686-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:650: note: candidates are: typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::reference std::vector<_Tp, _Alloc>::at(size_t) [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]
/usr/lib/gcc/i686-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:668: note:                 typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::const_reference std::vector<_Tp, _Alloc>::at(size_t) const [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]

vector::at expects an argument of type vector::size_type , which is typically an alias for size_t ; vector::at需要一个类型为vector::size_type的参数,通常是size_t的别名; this is exactly what the error messages are telling you. 这正是错误消息告诉您的内容。 You're trying to pass an instance of vector::iterator instead. 您尝试改为传递vector::iterator的实例。

In your example you already have an iterator to the vector element, simply dereference it and do the equality comparison. 在您的示例中,您已经有一个vector元素的迭代器,只需对其取消引用并进行相等性比较即可。

if (*i==entry)

You have bigger problems than that though, your entire for loop is basically undefined behavior waiting to happen. 但是,您遇到的问题比那还大,整个for循环基本上是未定义的行为,等待发生。 Say you pass an entry to addSymbol that doesn't match the first element in symbols . 假设你传递一个entryaddSymbol不中第一个元素相匹配symbols Your code causes the entry to be push_back 'd into symbols , which will cause i to be invalidated if the vector need to reallocate storage. 您的代码使该entrypush_back转换为symbols ,如果vector需要重新分配存储,这将使i无效。 The for loop then increments i , which is undefined behavior. 然后for循环将i递增,这是未定义的行为。 Moreover, do you want to keep iterating over the vector once you've added the element to the vector ? 此外,将元素添加到vector后,是否要继续迭代vector

I think what you want to do is check whether the vector contains any element that matches entry , and add it if it doesn't exist. 我认为您想要做的是检查vector是否包含与entry匹配的任何元素,如果不存在,请将其添加。 To do this, use find . 为此,请使用find

if(std::find(symbols.begin(), symbols.end(), entry) == symbols.end()) {
  // entry doesn't exist in the vector
  symbols.push_back(entry);
  symbolPos.push_back(position);
}

You do not need to call at with i, since i is an iterator, so a pointer.like object and you can acces the corresponding value via *i. 由于i是一个迭代器,因此您不需要使用i进行调用,因此可以使用一个类似指标的对象,并且可以通过* i访问相应的值。 You can use at with a integer index. 您可以将at与整数索引一起使用。

change this to 更改为

if (symbols.at(i)==entry)

to

if (symbols.at(*i)==entry)

Since this function is expecting an integer not a pointer. 由于此函数期望整数而不是指针。 :) :)

You are trying to pass an interator to the symbols vector 您正在尝试将Interator传递给符号向量

   if (symbols.at(i)==entry)

iterators are not like looping integer variables. 迭代器与循环整数变量不同。 so the code must be 所以代码必须是

   if (*i == entry)

if you want to use at then replace the for loop with integer value like 如果要使用at,则将for循环替换为整数值,例如

   for (i = 0; i < symbols.size; ++i)

Using iterators 使用迭代器

void addSymbol(string entry, int position){

// you can use find function instead of using your own loop
std::vector<string>::iterator i = find(symbols.begin(), symbols.end(), entry);

// if not found
if (i == symbols.end()){
symbols.push_back(entry);
symbolPos.push_back(position);
}
else
cout << "same entry" << endl;

}

There is no method "at" in vector which expects iterator as an input . vector中没有将迭代器作为输入的方法“ at”。 It expects size_type parameter . 它需要size_type参数。 Since you are using an iterator you can simply do strcmp(*i,entry) . 由于您正在使用迭代器,因此只需执行strcmp(* i,entry)即可。 Or another way is to simply switch from using iterator to using operator[] of vector ie 或者另一种方法是简单地从使用迭代器切换到使用vector的operator []

    for(int i=0;i<v.size();i++){
   cout<<i<<" th element : "<<v[i]<<endl;
   if(v[i]==entry){
    cout<<"Entry Already Exists"<<endl;
    break;
   }
 }

Another very easy way is to use "find" method , by which you can simply search for existence of entry in the present list 另一个非常简单的方法是使用“查找”方法,通过该方法,您可以简单地搜索当前列表中条目的存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM