在C ++一致性程序中使用向量时的分段错误11

时间:2014-12-15 00:43:12

标签: c++ macos vector segmentation-fault

对于初学者来说,这里总C ++和编码noob,提前道歉。我一直致力于一个程序,它创建一个文本文件的一致性,包括一个单词出现的次数以及该单词出现在哪些行上。预期输出的简要示例:

A在第1 3 5行发生了9次

AND在第2 4行发生3次

我首先只使用数组编写程序,我已成功运行。我现在正试图用向量而不是数组重写它,基本上我不知道我在做什么过去声明向量。我已经将我的矢量版本编译和链接没有错误,但是当我运行它时,我得到了一个"分段错误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();
    }

}

1 个答案:

答案 0 :(得分:0)

你如何填充你的载体? 使用push_back功能填写它们。

vector<int> v; // an empty container
v.push_back(10); // now it has one element - an integer 10

使用size()函数访问矢量大小,不要传递长度参数,就像在少数函数中一样(排序,搜索)。

迭代stl容器(vector是容器)的另一种方法是使用迭代器

for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{ int a = *it; /*gives you an element of the container*/ }

使用大小或迭代器将阻止您访问未分配的内存 - 这是您的问题

不检查向量边界时不要使用operator []。

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";