-1

I am writing a friend recommendation algorithm and in a part I have to store 350 random friendship using the data type std::pair in C++. I basically use an adjacency list (implemented as vector of vectors). I create a vector that stores data type pair<int,int>. I select a random value from the adjacency list and select one of it's friends randomly, however, even though I'm quite sure that I push the data type as pair however I cannot iterate through it.

What could be the possible reason?

int FRIENDS_AND_UNFRIENDS_TO_STORE=350,randomNode=rand()%adjacencyList.size(),randomFriend;
vector< pair<int,int> >listForPR;
listForPR.resize(FRIENDS_AND_UNFRIENDS_TO_STORE*2);

for(int i=0;i<FRIENDS_AND_UNFRIENDS_TO_STORE;i++) {
    while(adjacencyList[randomNode].size()<1)
        randomNode=rand()%adjacencyList.size();
    randomFriend=rand()%adjacencyList[randomNode].size();
    listForPR.push_back(make_pair(randomNode,adjacencyList[randomNode][randomFriend]));
}

for(int i=0;i<350;i++)
    cout<<"Node #"<<listForPR[i].first<<" & It's Friend: "<<listForPR[i].second<<endl;

Added this and !mysteriously solved the problem;

for(int i=0;i<FRIENDS_AND_UNFRIENDS_TO_STORE;i++) {
    while(adjacencyList[randomNode].size()<1)
        randomNode=rand()%adjacencyList.size();
    randomFriend=rand()%adjacencyList[randomNode].size();
    pair<int,int> temp=make_pair(randomNode,adjacencyList[randomNode][randomFriend]);//added
    listForPR.push_back(temp);
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Ali
  • 5,338
  • 12
  • 55
  • 78
  • 1
    Not sure what your problem is: you cannot iterate on a `pair<>`, it is not a collection (in the STL sense of the word collection) – Attila May 23 '12 at 18:13
  • My problem is; I have stored data type `x` in a vector, which has the properties of `.first` `.second`. And when I try to create a simple loop like `for(int i=0;i – Ali May 23 '12 at 18:16
  • Are you getting an error at compile time or at run time? If compile time, what is the error? If run time, then what is its behavior? – Edward Loper May 23 '12 at 18:31

2 Answers2

1

Your vector contains 350/2 = 175 elements that you pushed in during the loop, but you are going through 350 elements when you iterate.

qdii
  • 12,505
  • 10
  • 59
  • 116
  • You are definately right but I dont think it is a valid reason for me not being able to reach the **pair** data types properties – Ali May 23 '12 at 18:17
  • could you explain what you mean with “not being able to reach the pair data types properties”. Does the program crash ? – qdii May 23 '12 at 18:18
  • When I write a basic for loop `for(int i=0;i – Ali May 23 '12 at 18:20
  • Simply recognize the first function, but the IDE (Codeblocks is what im using) treates the vector[i] object as a vector, not a pair. – Ali May 23 '12 at 18:20
  • what you are showing should work, are you sure the error comes from here? – qdii May 23 '12 at 18:23
  • 1
    @rolandbishop - your posted code does not exhibit that problem. Are you sure you posted the right code (e.g. the right types for the variables)? You are accessing `.first` only wrt to `listForPR[i]`, which _is_ a `pair<>` – Attila May 23 '12 at 18:26
  • @Attila I know it seems strange. Actually I'm quite pissed that it made me work for a change that is actually not a change!. I've posted exactly(without any errors). Maybe its a make_pair related issue. I literally cannot explain but just say miracilously worked. Thanks for your contribution – Ali May 23 '12 at 18:32
  • @rolandbishop : by the way, you should have made clear when you edited your text that you changed the values (350 and 175). My answer seems rather curious now :P – qdii May 23 '12 at 18:33
0

You shouldn't mix resize and push_back.

The first resize fills listForPR with 350 zeroed items. Then push_back adds items to the end of the vector.

Deleting resize statement should fix the problem. Even better solution is to use reserve (it just prepares buffer in vector for insertion).

usamec
  • 2,156
  • 3
  • 20
  • 27