-1

I have a list of integers.(Currently stored in a std::vector but to increase efficieny, I need to convert it to set. But in current version, I use it as following: (I'm using c++98 not c++11)

int res=0;
vector<vector<int> >costMatrix;
vector<int>partialSolution;
    for(int i =0;i<partialSolution.size()-1;i++){
        res+=costMatrix[partialSolution.get(i)][partialSolution.get(i+1)];
    }

So, I need to do the same thing with the set data structure. But I dont know how to get two elements from the set at a time. I can get the partialSolution.get(i) with the code below but I could not get the partialSolution.get(i+1). Is there anyone to help me to modify the code below?

 // this time set<int> partialSolution
    int res=0;
    std::set<int>::iterator it;
    for (it = partialSolution.begin(); it != partialSolution.end(); ++it)
{
    res+=costMatrix[*it][]; 
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
zwlayer
  • 1,752
  • 1
  • 18
  • 41
  • 3
    "Currently stored in a `std::vector` but to increase efficiency, I need to convert it to `std:;set`" - that seems highly unlikely to improve performance, `std::set` is very cache-unfriendly. – BoBTFish Jan 15 '16 at 14:29
  • In somewhere else in my code, I'm searching whether my vector contains a specific number or not. Actually I have to change that part. But to change it I have to change the part I mentioned above too. @BoBTFish – zwlayer Jan 15 '16 at 14:30
  • are the integers sorted in `partialSolution`? – Karoly Horvath Jan 15 '16 at 14:31
  • 4
    If you expect the same behaviour from your `set` as your `vector`, I must assume your `vector` is sorted. Therefore, you may use [`std::lower_bound`](http://en.cppreference.com/w/cpp/algorithm/lower_bound) to efficiently find an existing element (or even [`std::binary_search`](http://en.cppreference.com/w/cpp/algorithm/binary_search) if you only need to know it exists, not access it). – BoBTFish Jan 15 '16 at 14:32
  • Have you tried `costMatrix[*it][*(std::next(it))]`? – jpo38 Jan 15 '16 at 14:32
  • @jpo38 when it points the last element isn't it cause an error ? – zwlayer Jan 15 '16 at 14:33
  • @jpo38 that's already c++11 – Slava Jan 15 '16 at 14:34
  • Sure, but you can iterate to std::prev(partialSolution.end()) – jpo38 Jan 15 '16 at 14:34
  • @BoBTFish rather than judging my aim, please help my *real* question if you can. – zwlayer Jan 15 '16 at 14:34
  • 5
    @zwlayer I'm trying to establish if this is an [XY Problem](http://meta.stackexchange.com/q/66377/242291). When the question is "How can I shoot myself in the foot?", the only correct answer is "Don't!" – BoBTFish Jan 15 '16 at 14:36
  • 2
    You should rather listen to what he's saying. There's a good chance `vector` will outperform your `set` attempts... – Karoly Horvath Jan 15 '16 at 14:36
  • @KarolyHorvath then let me try both and decide to which of them is better for me. – zwlayer Jan 15 '16 at 14:38
  • `std::set` is `O(log n)` inserts and indexing. `std::vector` is amortized `O(1)` inserts and indexing. Can't think of why you'd want a `std::set` for this task. – erip Jan 15 '16 at 14:46

1 Answers1

2

This could work (iterating from begin() to end()-1 and using std::next or ++ to get item next to current one).

In C++11:

for (it = partialSolution.begin(); it != std::prev(partialSolution.end()); ++it)
{
    res+=costMatrix[*it][*(std::next(it))]; 
}

In C++98:

std::set<int>::iterator last = partialSolution.end();
--last;
for (it = partialSolution.begin(); it != last; ++it)
{
    // not optimal but I'm trying to make it easy to understand...
    std::set<int>::iterator next = it;
    ++next;
    res+=costMatrix[*it][*next]; 
}
jpo38
  • 20,821
  • 10
  • 70
  • 151
  • 1
    it is c++11, OP asked for c++98 – Slava Jan 15 '16 at 14:35
  • I think the C++11 could be better with `std:accumulate`, but maybe not. – erip Jan 15 '16 at 14:49
  • @jpo38 do you know how could I represent this one also using a set? `for(int i=1;i – zwlayer Jan 15 '16 at 14:54
  • The same way, but just do `std::set::iterator prev = it; --prev;` and then do `answer+=costMatrix[partialSolution[*prev]][partialSolution[*it]];`. Loop has to start as `++begin()`. – jpo38 Jan 15 '16 at 14:55
  • @jpo38 couldnt figure out, if possible could you write it as an edit ? – zwlayer Jan 15 '16 at 14:58
  • @zwlayer You should accept this answer and post a separate question. My answer would not make sense as this question is not part of the original post... – jpo38 Jan 15 '16 at 15:11