Сортировка C++ и отслеживание индексов

Вышеуказанное решение не сработало для меня. Но это гораздо более простое решение прекрасно работает для меня

- (NSImage *)imageTintedWithColor:(NSColor *)tint
{
    NSImage *image = [self copy];
    if (tint) {
        [image lockFocus];
        [tint set];
        NSRect imageRect = {NSZeroPoint, [image size]};
        NSRectFillUsingOperation(imageRect, NSCompositeSourceAtop);
        [image unlockFocus];
    }
    return image;
}
202
задан Lightness Races with Monica 7 August 2015 в 07:30
поделиться

3 ответа

Если это возможно, вы можете построить массив позиций с помощью функции поиска, а затем отсортировать массив.

Или, может быть, вы можете использовать карту, где ключ будет элементом, а значения список своей позиции в следующих массивах (A, B и C)

Это зависит от дальнейшего использования этих массивов.

1
ответ дан 23 November 2019 в 05:00
поделиться

Are the items in the vector unique? If so, copy the vector, sort one of the copies with STL Sort then you can find which index each item had in the original vector.

If the vector is supposed to handle duplicate items, I think youre better of implementing your own sort routine.

1
ответ дан 23 November 2019 в 05:00
поделиться

You could sort std::pair instead of just ints - first int is original data, second int is original index. Then supply a comparator that only sorts on the first int. Example:

Your problem instance: v = [5 7 8]
New problem instance: v_prime = [<5,0>, <8,1>, <7,2>]

Sort the new problem instance using a comparator like:

typedef std::pair<int,int> mypair;
bool comparator ( const mypair& l, const mypair& r)
   { return l.first < r.first; }
// forgetting the syntax here but intent is clear enough

The result of std::sort on v_prime, using that comparator, should be:

v_prime = [<5,0>, <7,2>, <8,1>]

You can peel out the indices by walking the vector, grabbing .second from each std::pair.

85
ответ дан 23 November 2019 в 05:00
поделиться
Другие вопросы по тегам:

Похожие вопросы: