[英]Segmentation fault 11 when using vectors in C++ concordance program
對於初學者來說,這里的全部C ++和編碼菜鳥都表示歉意。 我一直在研究一個程序,該程序可以創建一個文本文件,該文件與單詞出現的次數和單詞出現的行數保持一致。 預期輸出的簡要示例:
在行1 3 5上發生9次
AND在第2 4行出現3次
我首先只使用成功運行的數組編寫程序。 我現在正嘗試使用向量而不是數組來重寫它,並且基本上我不知道在聲明向量之后我正在做什么。 我的矢量版本可以正確編譯和鏈接,但是運行時會出現“ segmentation fault 11”錯誤。 據我了解,發生此錯誤的原因是因為我正在嘗試訪問尚未分配的內存。 我要粘貼到目前為止所寫的全部代碼。 如果有人可以幫助我,或者讓我指責正確的導演來實現這一目標,那真是太棒了。 我知道我需要推送push_back方法,但是我不知道在哪里。 我意識到這對大多數人來說可能是基本的,但是我只是想把所有這些都包裹住。 再一次,非常感謝-
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
void copy(vector<int> fromarr, vector< vector<int> > toarr, int index)
{
for (int i = 0; i <= fromarr[0]; i++)
{
toarr[index][i] = fromarr[i];
}
}
void copy(vector< vector<int> > fromarr, vector<int> toarr, int index)
{
for (int i = 0; i <= fromarr[index][0]; i++)
{
toarr[i] = fromarr[index][i];
}
}
int search(vector<string> array, int len, string target)
{
for(int i = 0; i < len; i++)
{
if(array[i] == target) return i;
}
return -1;
}
void sort(vector<string> wordarray, vector<int> linecount, vector< vector<int> > linenumbersarray, int length)
{
int minpos = 0;
for(int i = 0; i < length; i++)
{
minpos = i;
for (int j = 0; j < length; j++)
{
if(wordarray[j] > wordarray[minpos]) minpos = j;
string tempword = wordarray[i];
int tempcount = linecount[i];
vector<int> tempnums;
copy(linenumbersarray, tempnums, i);
wordarray[i] = wordarray[minpos];
linecount[i] = linecount[minpos];
copy(linenumbersarray[minpos], linenumbersarray, i);
wordarray[minpos] = tempword;
linecount[minpos] = tempcount;
copy(tempnums, linenumbersarray, minpos);
}
}
}
int main(int argc, char* argv[])
{
vector<string> wordarray;
vector<int> linecount;
vector< vector<int> > linenumbersarray;
int arrayposition = 0;
int linenumber = 1;
int wordlength = 0;
ifstream infile;
infile.open(argv[1]);
string aline;
while (getline(infile, aline))
{
istringstream theline(aline);
string aword;
while (theline >> aword)
{
int isupdated = search(wordarray, wordlength, aword);
if (isupdated == -1)
{
wordarray[wordlength] = aword;
linecount[wordlength] = 1;
linenumbersarray[wordlength][0] = 1;
linenumbersarray[wordlength][1] = linenumber;
wordlength = wordlength + 1;
}
else
{
linecount[isupdated] = linecount[isupdated] + 1;
if (linenumbersarray[isupdated][linenumbersarray[isupdated][0]] != linenumber)
(linenumbersarray[isupdated][++linenumbersarray[isupdated][0]] = linenumber);
}
}
linenumber = linenumber + 1;
}
sort(wordarray, linecount, linenumbersarray, wordlength);
for (int i = 0; i < wordlength; i++)
{
ostringstream out;
for (int j = 1; j <= linenumbersarray[i][0]; j++)
{
out << linenumbersarray[i][j];
j != linenumbersarray[i][0] ? out << " " : out << ".";
}
cout << wordarray[i] << " occurs " << linecount[i] << " time(s) on lines " << out.str() << endl;
out.flush();
}
}
您如何填充向量? 使用push_back函數進行填寫。
vector<int> v; // an empty container
v.push_back(10); // now it has one element - an integer 10
使用size()函數訪問矢量大小, 不要像在少數函數(排序,搜索)中那樣傳遞長度參數 。
遍歷stl容器(向量是容器)的另一種方法是使用迭代器 。
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{ int a = *it; /*gives you an element of the container*/ }
使用size或迭代器將阻止您訪問未分配的內存- 這是您的問題 。
在不檢查向量邊界的情況下,請勿使用運算符[]。
if (i < v.size()) {v[i]; //an element}
這是您搜索功能的兩個變體
int search(vector<string> const &array, string const &target)
{
for(int i = 0; i < array.size(); i++)
{
if(array[i] == target) return i;
}
return -1;
}
vector<string>::const_iterator search(vector<string> const &array, string const &target)
{
for(vector<string>::const_iterator it = array.begin(); it != array.end(); ++it)
{
if(*it == target) return it;
}
return array.end();
}
更好的搜索方法是使用std :: find函數,但是當您更熟悉STL時,可以將其留待以后使用。 std :: find與上述搜索的第二種變體相同。
vector<string>::iterator it;
it = find (myvector.begin(), myvector.end(), target);
if (it != myvector.end())
std::cout << "Element found in myvector: " << *it << '\n';
else
std::cout << "Element not found in myvector\n";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.