473,804 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1509
In article <41************ ***********@new s.skynet.be>,
slurper <sl*********@sk ynet.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*******@pres by.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::iterato r iVec_i;
typedef std::set<int> iSet_t;
typedef std::map<int,iS et_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.en d());

// 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::iterato r iVec_i;
typedef std::set<int> iSet_t;
typedef std::map<int,iS et_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.en d());

// 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
5029
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 points in space that arrive randomly into groupings based on (x, y, z) dimensions, so I use std::map to do that. Once the sorting is done I need to find the interactions between groups, so I've been using iterators to step through the map. As a...
5
2478
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 lower to upper. Now I want to remove all values from the stl vector in range having values in . I have just tried around with remove_if and some predicates, but I'm not used to the algorithms library. Perhaps someone can help me.
11
2907
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 circumstances in which the STL will move a vector, or invalidate iterators to elements in the vector, if you don't insert or remove elements? My actual problem seems to be as follows: I have class X, which contains an STL vector. The constructor...
6
3129
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
4224
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
4160
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 vector. Any ideas? Thanks in advance, Fred.
13
20166
by: zaineb | last post by:
Hi, This is a follow-up of sort of this thread: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/de12af6539e0d645/662cab752779f1fa?lnk=st&q=c%2B%2B+vector+vs+map&rnum=1&hl=en#662cab752779f1fa I've been chewing on this problem for a while. When I came across the above thread, I figured that maybe other members have a better idea on how to approach this. I'm currently trying to decide between which one of maps or...
3
9369
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 ind_len.push_back() call, at the second iteration. I suspect that the iterator has been invalidated, but I can't see how this would be from one iteration to another in this tiny for loop. Could anyone give me more insight? Thanks!
4
11497
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(), less<int>()); to sort the vector x. Then the sorted x becomes {0, 2, 2, 3, 5}. But how can I obtain the indices of sorted x, which is {3, 1, 4, 2, 0}.
9
3763
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" contains built-in types (e.g. int), then "clear()" doesn't remove anything at all. Why does "clear()" have this behaviour? Also, when I copy one vector "v1" from another vector "v2", with "v1" longer than "v2" (e.g. "v1" has 2 elements and "v2" has...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.