[英]Indexing a Vector C++
我是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);
}
}
}
我的编译器抛出错误,说找不到.at()。 我在这里做错了什么?
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
需要一个类型为vector::size_type
的参数,通常是size_t
的别名; 这正是错误消息告诉您的内容。 您尝试改为传递vector::iterator
的实例。
在您的示例中,您已经有一个vector
元素的迭代器,只需对其取消引用并进行相等性比较即可。
if (*i==entry)
但是,您遇到的问题比那还大,整个for
循环基本上是未定义的行为,等待发生。 假设你传递一个entry
来addSymbol
不中第一个元素相匹配symbols
。 您的代码使该entry
被push_back
转换为symbols
,如果vector
需要重新分配存储,这将使i
无效。 然后for
循环将i
递增,这是未定义的行为。 此外,将元素添加到vector
后,是否要继续迭代vector
?
我认为您想要做的是检查vector
是否包含与entry
匹配的任何元素,如果不存在,请将其添加。 为此,请使用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);
}
由于i是一个迭代器,因此您不需要使用i进行调用,因此可以使用一个类似指标的对象,并且可以通过* i访问相应的值。 您可以将at与整数索引一起使用。
更改为
if (symbols.at(i)==entry)
至
if (symbols.at(*i)==entry)
由于此函数期望整数而不是指针。 :)
您正在尝试将Interator传递给符号向量
if (symbols.at(i)==entry)
迭代器与循环整数变量不同。 所以代码必须是
if (*i == entry)
如果要使用at,则将for循环替换为整数值,例如
for (i = 0; i < symbols.size; ++i)
使用迭代器
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;
}
vector中没有将迭代器作为输入的方法“ at”。 它需要size_type参数。 由于您正在使用迭代器,因此只需执行strcmp(* i,entry)即可。 或者另一种方法是简单地从使用迭代器切换到使用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;
}
}
另一个非常简单的方法是使用“查找”方法,通过该方法,您可以简单地搜索当前列表中条目的存在
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.