473,378 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

vector indices

tx for answer Bazarov

i have another problem

i have a vector with 20 elements in it (20 ints).

i'd like to have the elements sorted in ascending order, but somehow i need
to know what index the element was at in the original vector.

someone?
tx for quick and good answers so far

Jul 22 '05 #1
4 1489
In article <41***********************@news.skynet.be>,
slurper <sl*********@skynet.be> wrote:

i have a vector with 20 elements in it (20 ints).

i'd like to have the elements sorted in ascending order, but somehow i need
to know what index the element was at in the original vector.


Create a second vector of 20 ints, and initialize it so it elements are
the same as the indexes, i.e. v2[0] == 0, v2[1] === 1, etc. Let's call
this an "index vector".

Write a sort function using your favorite sort algorithm. Whenever you
move alements around in the data vector in the process of sorting it (such
as swap two elements), do the same thing to the corresponding elements of
the index vector.

After this function executes, the element at position k of the data vector
will have originally been in position v2[k] of the unsorted data vector.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #2
slurper wrote in news:41***********************@news.skynet.be in
comp.lang.c++:
tx for answer Bazarov

i have another problem

i have a vector with 20 elements in it (20 ints).

i'd like to have the elements sorted in ascending order, but somehow i
need to know what index the element was at in the original vector.

someone?
tx for quick and good answers so far


#include <cstdlib>
#include <utility>
#include <vector>
#include <algorithm>

std::vector< std::pair< int, std::size_t > >
sort_and_index( std::vector< int > const &in )
{
std::vector< std::pair< int, std::size_t > > out( in.size() );

for ( std::size_t i = 0; i < in.size(); ++i )
{
out[ i ].first = in[ i ];
out[ i ].second = i;
}

std::sort( out.begin(), out.end() );

return out;
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
Maybe something like this (probably overkill but hey STL is fun)...

#include<map>
#include<set>
#include<vector>

typedef std::vector<int> iVec_t;
typedef iVec_t::iterator iVec_i;
typedef std::set<int> iSet_t;
typedef std::map<int,iSet_t> isMap_t;

iVec_t data;

// load and populate data with your data

isMap_t indicies;

for( iVec_i di = data.begin() ; di != data.end() ; ++di )
indicies[*di].insert(di-data.begin());

std::sort(data.begin(),data.end());

// Now you have sorted data, and the indicies map will map
// your data values to a set of indicies in the original
// vector where that data was found.
// The set in the map allows for data values to appear more
// than once in the original vector.
slurper wrote:
tx for answer Bazarov

i have another problem

i have a vector with 20 elements in it (20 ints).

i'd like to have the elements sorted in ascending order, but somehow i need
to know what index the element was at in the original vector.

someone?
tx for quick and good answers so far

Jul 22 '05 #4
Maybe something like this (probably overkill but hey STL is fun)...

#include<map>
#include<set>
#include<vector>

typedef std::vector<int> iVec_t;
typedef iVec_t::iterator iVec_i;
typedef std::set<int> iSet_t;
typedef std::map<int,iSet_t> isMap_t;

iVec_t data;

// load and populate data with your data

isMap_t indicies;

for( iVec_i di = data.begin() ; di != data.end() ; ++di )
indicies[*di].insert(di-data.begin());

std::sort(data.begin(),data.end());

// Now you have sorted data, and the indicies map will map
// your data values to a set of indicies in the original
// vector where that data was found.
// The set in the map allows for data values to appear more
// than once in the original vector.
slurper wrote:
tx for answer Bazarov

i have another problem

i have a vector with 20 elements in it (20 ints).

i'd like to have the elements sorted in ascending order, but somehow i need
to know what index the element was at in the original vector.

someone?
tx for quick and good answers so far

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Jim West | last post by:
I'm curious why I might be getting such a large performance difference between using a map iterator and a vector iterator. This is a computational electromagnetics code where I have to separate...
5
by: Alexander Stippler | last post by:
Hello, I have got a list of indices stored as a stl::vector and a range given by two iterators, lets say . The values in this range are not ordered, but I have another range of values, lets say...
11
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
6
by: Matthias | last post by:
Hi, say I have a vector v1: std::vector<SomeType> v1; and I need a vector v2 of pointers to v1's elements: std::vector<SomeType*> v2;
6
by: hannibal200480 | last post by:
Hi everyone, Here about what it is. vector<vector<Point3D> > pVector0; pVector0 is a vector of vector of 3D points. pVector0 is a vector of 3D points.
2
by: fred_stevens | last post by:
Hi all you C boffins: I need to sort a vector of doubles is ascending order. Qsort will return the sorted vector, but I need a vector of the indices of the sorted vector, not the actual sorted...
13
by: zaineb | last post by:
Hi, This is a follow-up of sort of this thread:...
3
by: ngaloppo | last post by:
Hi, compiling a program with the code blurb below causes a runtime error ("Expression: vector iterators incompatible") due to the debug iterators in VC++ 8. The error happens in the...
4
by: boheman | last post by:
Hi, I am wondering if there is a simple and quick way to return the indices of sorted vector. for example, I have a vector<intx containing {5, 2, 3, 0, 2}. I can use sort(x.begin(), x.end(),...
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.