finding location of array

I have a array let say

string first [10];
string second [10];

now using cin a person can type some string into a chosen location.

Now I want to order this array so I've used

1
2
3
4
5
6
7
8
9
10
11
12
13

for (int i=0; i<=9; i++)
	{
		second[i] = first[i];
	}

sort (second, second + 9);

for (int i=0; i<=9; i++)
	{
		cout << second[i] << endl;
	}


So this array alphabetically orders the array of second. Which is fine. It works. But what I want to do is find the position in the first array that the text was entered and cout it.

For example

I enter "zenda" in location 7 of "first" array.

After first array gets coppied to second array. I want it to display

Zenda is in location 7.
...
Beta is in location 8.

And so on
You may want to consider using a std::map<std::string, int> to hold the string and the index location.
@jilb
If it's a school assignment I'm sure skipping straight to the finish line of STLs would be discouraged. Though... Yes maps are delicious for direct LookUp.

Ok so, if I'm understanding this right you want to be able to sort the list of data but still retain where it was initially put into the array?

The only way to really retain that information is to create a variable for it.

1
2
3
4
5
struct Name
{
	string m_name;
	int m_position;
};


That way your input will always be linked to the original position. There are a ton of ways to go about what you're wanting and I'm *hoping* that I understood what you were wanting to do.
Last edited on
If it's a school assignment I'm sure skipping straight to the finish line of STLs would be discouraged.

True but most school assignments also discourage the use of std::sort as well. So not knowing the actual limits of the assignment who knows even structures may be off limits.?

you're right. My eyes just went past sort. I thought it was his own defined function. I think we've been had.
Last edited on
Yeap its a school assignment but theres no limits.

Structs/OO is the second part of the project :)
Topic archived. No new replies allowed.