vector of pointers

I need to write a program that reads in a text file of 1000 words, stores the address of each word into a different element in a vector of pointers, and store each unique word into an array of strings. I understand the principles behind vectors and pointers and arrays, I'm just having trouble knowing where to start and what I am doing wrong. Any suggestions on what to do next?

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<vector>
using namespace std;
const int SIZE=200;

int main()
{
ifstream fin;
fin.open("word.txt");
if(fin.fail())
{cout<<"File did not open. Shutting down...\n\a";
return 0;
}//if

string word;
int currentSize=1;
string unique[SIZE];

vector<string*> ptr(0);


while(fin>>word)
{
ptr.push_back(&word);
for(int count=0; count<currentSize; count++)
{

cout<<ptr[count];
if(word!=*ptr[count]){unique[count]=word;
currentSize++;}

}


}


return 0;
}//main
A vector of pointers is rarely a useful idea, and parallel arrays are even less common. What do you need this data structure for? How is it going to be used?

If you're looking to list all unique words, consider a set:

1
2
3
4
5
6
7
8
9
10
11
12
#include <fstream>
#include <string>
#include <iterator>
#include <set>

using namespace std;
int main()
{
    ifstream fin("word.txt");
    istream_iterator<string> beg(fin), end;
    set<string> unique(beg, end);
}

Yes.. It's not very practical. It's an assignment designed to test my knowledge of pointers and vectors. It's very limited in its application. I'm struggling the most with the best way to compare the strings as it reads in each word to determine whether the word is unique and has already been stored in the array or whether it is a duplicate of a previous word.
closed account (z05DSL3A)
It sounds a bit like the Flyweight pattern but thats not important.

The basic flow would be something like:
Read a word from the file.
Add the word to the array and get a pointer to the element in the array
Store the pointer in the vector.

Breaking Down the Add the word to the array...
With the string you loop through (iterate) the array comparing the the string to the element
if the string is matched to one in the array take a pointer to that element
else add the string to the next available position in the array and take a pointer to that element.
This is what I have so far.. I'm getting an error, though, and I'm not sure where from. I'm also stuck as to what to do next.

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<vector>
using namespace std;
const int SIZE=200;

int main()
{
ifstream fin;
fin.open("word.txt");
if(fin.fail())
{cout<<"File did not open. Shutting down...\n\a";
return 0;
}//if

string word;
int currentSize=0;
string unique[SIZE];

vector<string*> ptr(0);


for(int count=0; count<1000; count++)
{
fin>>word;
unique[count]=word;

currentSize++;
for(int index=0; index<currentSize; index++)
{
if(unique[count]==unique[index])
{
ptr.push_back(&unique[count]);
}//if
else
{ptr.push_back(&unique[count+1]);}//else
}
}


return 0;
}//main
Consider stepping through your loop with a debugger and observing the results of each line. That should tell you at least whether it stores unique or all strings in the array,

I think you're shooting for something like

1
2
3
4
5
6
7
int count = 0;
for(string word; fin >> word && count < SIZE; ++count)
{
    ptr.push_back(find(unique, unique+count, word));
    if(ptr.back() == unique+count)
        unique[count] = word;
}


Although you could improve the efficiency of the search for duplicates by using an ordered container instead of array.
Last edited on
This is where I'm at now.

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<vector>
using namespace std;
const int SIZE=200;

int findVal(string[], int, string);
void sortVector(vector<string*> &);
int main()
{
ifstream fin;
fin.open("word.txt");
if(fin.fail())
{cout<<"File did not open. Shutting down...\n\a";
return 0;
}//if

string word;
int currentSize=0;
string unique[SIZE];

vector<string*> ptr(0);


for(int count=0; count<1000; count++)
{
fin>>word;

int find=findVal(unique, currentSize, word);
if(find==-1)
{
unique[currentSize]=word;
ptr.push_back(&unique[currentSize]);
currentSize++;
}//if
else
{
ptr.push_back(&unique[find]);

}//else
}

sortVector(ptr);


return 0;
}//main


int findVal(string unique[], int currentSize, string word){
for(int index=0; index<currentSize; index++)
{
if(word==unique[index])
{
return index;
}//if
}//for
return -1;
}//findVal
void sortVector(vector<string*> &ptr)
{
string temp;
bool swap;
do
{ swap=false;
for(unsigned count=0; count<ptr.size()-1; count++)
{
if(ptr[count]>ptr[count+1])
{
temp=*ptr[count];
ptr[count]=ptr[count+1];
*ptr[count+1]=temp;
swap=true;
}//if
}//for
}while(swap);
}//sortVector
Topic archived. No new replies allowed.