Creating a Vector of Strings

Hi, I was trying to create a vector of strings with size 100 using the following code:

 
vector<string> arr(100);


However, this line gives me an error - expected parameter declarator. Why is this an error, and how do I rectify it? I haven't experienced this error while working with vectors of integers, bools, etc.
Last edited on
Did you #include all the required headers, ie string, vector?

How did you qualify the scope of those two classes?
Show the surrounding code.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> vector_of_strings(100);
    
    std::cout << vector_of_strings.size() << '\n';
    
    return 0;
}
@mbozzi
Here is the entire code. Line 11 is the one that's giving me a problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using std::vector;
using std::string;

class Phonebook{
    private:
    vector<string> Contacts(100);

    public:
    Phonebook() {}

    void add(int n, string name){
        Contacts[n] = name;
    }

    void del(int n){
        Contacts[n].clear();
    }

    void find(int n){
        string o{ Contacts[n]};
        if(o==""){
            cout<<"not found"<<endl;
        }
        else{
            cout<<o<<endl;
        }
    }

    void processqueries(int N){
        string str;
        string n;
        int no;
        for(int i=0;i<N;i++){
            cin>>str;
            if(str=="add"){
                cin>>no >> n;
                add(no,n);
            }
            if(str=="find"){
                cin>>no;
                find(no);
            }
            if(str=="del"){
                cin>>no;
                del(no); 
            }
        }
    }
};

int main(){
    int N;
    Phonebook ph;
    cin>>N;
    ph.processqueries(N);
    return 0;
}


Last edited on
@jib
Yes. I did #include the required headers. And then I used the using directive:

1
2
3
4
5
6
7
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using std::vector;
using std::string;


Could this be a problem because @againtry 's code runs fine.
Last edited on
https://stackoverflow.com/questions/28968226/defining-a-vector-with-fixed-size-inside-a-class-in-c

You could either:
- not use a fixed-size vector (this would be my preference);
- use a different type of container;
- size it in the class constructor;
- bizarrely, you could do:
vector<string> Contacts = vector<string>(100);
but I don't understand why this compiles and the original didn't.
Last edited on
The reason againtry's code works is because the declaration is outside the class, to make it work in the class you need to use braces instead of parentheses.

Also note that the zero argument constructor is created by default so there is no need to create this constructor as a do nothing function.
Last edited on
@OP
Well, now that the vector is sorted there are a number of problems going way beyond that now we know the context. And you will get more use/fun out of your phonebook:

1. Why specify the size anyway? Let the <vector> work that out.
2. Why select a vector? A <map> could be better
3. Why just store names? Why not Person objects with a name and phone-number at least?
4. clear() clears the whole vector, not just the one at the index -> erase() is more likely
5. a find() function would be very useful - i.e search for a name ...
6. use push_back() and pop_back() <vector> functionality perhaps? Who cares what the index is? (Even though it's useful to avoid duplicates)
Thank you all for clearing my doubt and helping me out.

@againtry

Thank you for those points. I'd like to learn more about <map>. Is there any book or other resource you'd recommend me to refer to, so that I get a better idea.
Actually, the problem was for us to try out direct accessing and store the name of the person at the index = phone number. I wanted to try it out for a hypothetical case where all phone numbers were at max 2 digits long. At the end we concluded that this method isn't useful because real life numbers can be 10-12 digits long.
@viraamrao
For a good start on this and other "(STL) containers" you can't go past the tutorials and references on this site. They all come with short working sample code.

For <map>'s: http://www.cplusplus.com/reference/map/

Generally: http://www.cplusplus.com/doc/
and, learncpp is another good free site
https://www.learncpp.com/cpp-tutorial/16-3-stl-iterators-overview/

Direct access via the key (could be a phone_no string) makes a map ideal


Topic archived. No new replies allowed.