Vector pointer of char

Im having some trouble with this concept. I need to make a program that keeps on a asking the user for a string. Takes that string and puts it in a vector of char, the program then needs to check if the word is already contained in that vector and point to that memory location. If anyone could help me out with the algorithm that would be great.

Thanks.
You have to change this code to work with char pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    typedef vector<string> Vector;
    Vector input_vector;
    string input;
    
    cout << "Type a string: ";
    while (getline(cin, input)) {
        Vector::iterator it = find(input_vector.begin(),
                                   input_vector.end(),
                                   input);
        if (it == input_vector.end()) {
            input_vector.push_back(input);
        } else {
            // it is "pointing" to the found string
            cout << "Already in vector: " << *it << endl;
            break;
        }
    }
}


Edit:

this is not so trivial as you might think first, because you possibly need to allocate dynamicly memory. (strdup could be helpful)
Last edited on
There is the problem to consider - that if more than one pointer points to the same item that was dynamically allocated using new and you delete one of them - it will mess things up for the others.
Topic archived. No new replies allowed.