[英]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.