Hello ALL,

I have a vector

vector<CLUSTER> ClustList; where

struct CLUSTER{
    string DocID_DocName;
}

some members of CLUSTER have been ignored for simplicityInitially, I put 3 elements into ClusterList by using push_back()
the content in DocID_DocName is shown below.

ClusterList[0].DocID_DocName = "0";
ClusterList[1].DocID_DocName = "1";
ClusterList[2].DocID_DocName = "2";

In the following program snippet, I want to update the content of ClusterList[0].DocID_DocName, ClusterList[1].DocID_DocName and ClusterList[2].DocID_DocName
with iterator

vector<CLUSTER>::iterator it;
for(it = ClusterList.begin(); it != ClusterList.end(); it++)
{
     // do mapping, so that I get DocName according to DocID
     // DocID    DocName
     //  0            NewsTilte.txt
     //  1            NewsProvider.txt
     // 2             NewsUser.txt
    String Str //mapping result is stored into Str
    (*it).DocID_DocName = Str;// cause segment fault.
}

The statement
(*it).DocID_DocName = Str;
caused segment fault.

The length of Str is longer than that of the origianl content.

But, I still cannot figure out the reason.
Please help. Thanks

Could you try this simple program and tell me if it has any errors?

#include <iostream>
#include <vector>
using namespace std;

struct cluster {
    string name;
};

int main() {
    vector <cluster> clusters (3);

    clusters[0].name = "test";

    cout << clusters[0].name << endl;

    string overwrite("OH YEAH!");

    clusters[0].name = overwrite;

    cout << clusters[0].name << endl;
    return 0;
}

This is a program to narrow down the problem: Is it really the assignment that's causing errors? If it is, this program should also throw a segfault. We'll see. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.