I was told you can access the index of an array and save it in a variable. Like, as you know, every element is referenced by an index number. 0,1,2,3,4 etc.. I don't want to save the content, I need the index number. I will need to know the position of each element later in the program. I need to know where it is.I'm not sure if I'm explaining it correctly. Anyway, how do I do this?

Edited 4 Years Ago by XodoX

So let me get this right. Uhh you want to check the position of specific values in the array and keep track of their position?

I'd probably use std::multimap from the <map> header.

    int TestArray[11] = {3, 9, 5, 1, 4, 2, 7, 8, 11, 10, 4};

    multimap<int, int> Tracker;
    for (int I = 0; I < 11; I++)
        Tracker.insert(pair<int, int>(TestArray[I], I));  //I will be the index.. The values will be the keys. You search for a value, it returns an index.

    multimap<int, int>::iterator it;
    it = Tracker.find(4);
    cout<< it->second <<endl;                    //Using an iterator to access values.

    cout<< (++it)->second <<endl;                    //This will print the second occurence of the value 4.

    cout<< Tracker.find(4)->second <<endl;      //Using an iterator indirectly to access values.

    cout<< (++Tracker.find(4))->second <<endl;  //Finds the second occurence of the value 4.

    cout<< Tracker.find(5)->second <<endl;      //Finds the occurence of the value 5.

All you have to do is enter the value you wish to search for, and it will return the position/index of it.

Edited 4 Years Ago by triumphost

Ok, I only need to put it into a variable.

int TestArray[11] = {3, 9, 5, 1, 4, 2, 7, 8, 11, 10, 4};
multimap<int, int> Tracker;
for (int I = 0; I < 11; I++)
Tracker.insert(pair<int, int>(TestArray[I], I));

I suppose that's what I need. So multimap gets the index here and stores it in Tracker? I'm not sure what the last line does here. But looks like that's actually the line that gets the index.

It uses a for loop, pairs the index with its value and inserts it into the multimap.

You find the value you want and the multimap will give you the index.

Edited 4 Years Ago by triumphost

Start
Word 1
Word 2
Word 3
End

Ok, the input consists of 1 word per line followed by a number. Each segment starts with "Start" and end with "End". I need to read it into an array and keep track of each line for later use so that I will know, like, "word 10" is in line 4 etc. You know? Then it needs to create an array equal to the number of the index ( lines).

string Store_Array[100];// stores string (word)
string operation[STORE_Array];

int Para_Array[50];//stores parameter (#)
int parameter[Para_Array;

int Track_Array[50];
int programcounter[Track_Array];

I have no idea how to do this. So the numbers and words are in there now. So now it needs to create Track_Array equal to the number of lines. Or do I have to have the Track_Array already in place? I don't even know if it can create an array itself.

Read a file into a vector.. For each line in the file, add it to the tracker and keep track of it's index.

Example File:

Start
Word 1
Word 2
Word 3
Word 4
End

It doesn't ignore the "Start" and "End" which you can remove easily so that it ignores. It reads that file, and keeps track of which lines the word was found on and fills a vector with the words, and a tracker with the indicies.

void PrintMultiMapKeyIndicies(std::multimap<std::string, int> &Tracker, const std::string &Key)
{
    std::multimap<std::string, int>::iterator it;
    std::multimap<std::string, int>::iterator last;

    it = Tracker.find(Key);
    if (it == Tracker.end())
    {
        std::cout<< "Key Not Found!" <<std::endl;
        return;
    }

    std::cout<< "The Key: "<< Key <<" is located at the following Indicies:\n" <<std::endl;
    last = Tracker.upper_bound(Key);

    for (; it != last; ++it)
        std::cout<< it->second <<std::endl;
}

int main()
{
    std::vector<std::string> FileContents;
    std::ifstream File("C:/Users/UserName/Desktop/SomeFile.txt", std::ios::in/*| std::ios::binary*/);
    if (File.is_open())
    {
        std::string Line;
        while (std::getline(File, Line))
            FileContents.push_back(Line);
        File.close();
    }

    std::multimap<std::string, int> Tracker;
    for (size_t I = 0; I < FileContents.size(); I++)
        //if (FileContents[I] != "Start" && FileContents[I] != "End")
            Tracker.insert(std::pair<std::string, int>(FileContents[I], I));

    PrintMultiMapKeyIndicies(Tracker, "Word 2");
}

Edited 4 Years Ago by triumphost

Comments
Thank you

The start would be index 0, the first word index 1 etc.

So you save the Indicies in Key here? It's jsut suppsoed to save it, so the output can be removed. Once I have it saved, I need to use the number of lines to manipulate the 3rd array so that it will be the size of key. So how would I use this to manipulate the 3rd array? That's why I wanted to use arrays and not vectors. I thought it would be easier to take the value and manipulate the 3rd array if it's all arrays.

Here's a quick and dirty way to store the items position. I'm using a vector for that.

#include <iostream>
#include <vector>
using namespace std;

int main(){
    int numbers[100];
    vector<int> divisors;
    for (int i=0;i<100;i++){
        numbers[i]=i+200;
    }
    for (int i=0;i<100;i++){
        if (numbers[i]%7==0) divisors.push_back(i);
    }

    for (int i=0;i<(int)divisors.size();i++){
        cout<<"Number divisible with 7: "<<numbers[divisors[i]]<<"\nPosition in the array: "<<divisors[i]<<endl;
    }
    return (0);
}

Now you can extend this method to apply to other forms of data structure.

I don't understand what you're asking. I provided an example of what will need to be done.

It works like this:
Reads Information from the file into a vector.
Each indicies in the vector will contain 1 line from the file.
Thus in the example, the vector is of size 6 and contains 6 lines including Start and End.

Tracker will then of course be size 6 as well and the tracker contains a line number and the contents of the line.

So from my example: tracker[0] is "Start", tracker[1] is "Word 1", tracker[2] is "Word 2"... tracker[5] is "End".. etc.

To find out the index of a line, you simply do:

Tracker.find("Word 1")->second    //This will give you index 1 aka line 1 in my example.
Tracker.find("Word 4")->second    //This will give you index 4 aka line 4 in my example.
Tracker.find("End")->second       //This will give you whatever inidices contain the word "End"

//If tracker contains more than one of the same word, it will print the index of every where that it's found.

PrintMultiMapKeyIndicies(Tracker, "Word 4");  //Will print every line Number that is "Word 4"

What you do with the code provided is up to you. It doesn't save the indicies in the key. It saves the contents in the key and when you search for that content, it will give you a line Number/index.

If you want to make a vector or array the same size as the amount of lines found, you do:

std::vector<int> ThirdArray(Tracker.size());


int* ThirdArray = new int[Tracker.size()];
//Use ThirdArray.
delete[] ThirdArray;

The difference is vector doesn't need cleaning up whereas the array will need cleaning up via delete[].

Edited 4 Years Ago by triumphost

This article has been dead for over six months. Start a new discussion instead.