Vector, string, int, and parameters.

Hey all, I've been writing a program for a week. I have a class with private string vectors and a few public set and get functions to set and call them up. This program is going to read a file and store the info in the different vectors in the class. Also if its any consolation it's written in three parts; header, cpp, and main.

This is where it gets tricky (at least for me)

I haven't a clue how to set up a function that calls the vector from main and has a int parameter to call a specific spot within the vector.



Can you clarify; this is a vector of strings, yes?

If so, a function like this would do in your class.
1
2
3
4
5
6
7
8
9
// Assuming vector object called myVector in class
std::string GetStringAtIndex( unsigned int idx )
{
   return idx < myVector.size() ? myVector[idx] : " ";
}

// Example call
// Assuming you have an instance of your class
std::string someString = yourObject.GetStringAtIndex( 0 );


Last edited on
1
2
3
4
5
6
7
8
9
10
11
//in main

std::vector<int>myvect;
int pos_in_vec = 3;

somefunc(&myvect, pos_in_vec)

somefunc(std::vector<int>*vect, int num)
{
//....
}
@smac89: I'd recommend passing in a reference to the vector, rather than a pointer to it:

1
2
3
4
5
6
7
8
9
10
11
//in main

std::vector<int>myvect;
int pos_in_vec = 3;

somefunc(myvect, pos_in_vec)

somefunc(std::vector<int>& vect, int num)
{
//....
}


That way somefunc doesn't need to check for a null pointer before attempting to use the vector.
Don't missunderstand the usage of references and pointers.

Of course, even if you pass it by reference, it could be null!

passing by reference, passing by value and passing by pointer are topics quite far from what the post did ask.
If you want to extract it from instances, you may do something like

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
class foo
{
public:
foo(){}

~foo(){}
std::vector<std::string> GetVector()
{
return myVector;
}
private:
std::vector<std::string>myVector;
 
}

//In Main

foo* myFoo = new foo();

for(int i = 0; i<10; i++)
myFoo->GetVector().push_back( itoa(i)); //This fills your vector.

std::string myString = getStringAtIndex(myFoo, 2);

//outside main

std::string getStringAtIndex(foo* fooToParse, unsigned int idx)
{
   return idx < fooToParse->GetVector().size() ? fooToParse->GetVector()[idx] : "ERROR!";
}

//If you prefer:


std::string getStringAtIndex(foo* fooToParse, unsigned int idx)
{
if(idx>fooToParse->GetVector().size())
return "ERROR!";
else
return fooToParse->GetVector()[idx] ;
   
}

Last edited on
If you want to use references instead of pointers, you may change the way you instance your objects, or even how you pass values to your functions

using references:

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
class foo
{
public:
foo(){}

~foo(){}
std::vector<std::string> GetVector()
{
return myVector;
}
private:
std::vector<std::string>myVector;
 
}

//In Main

foo* myFoo = new foo();

for(int i = 0; i<10; i++)
myFoo->GetVector().push_back(itoa(i)); //This fills your vector.

std::string myString = getStringAtIndex(*myFoo, 2);

//this is using references, and allocating in heap trough "new"
//It could be like:

foo myFoo;

for(int i = 0; i<10; i++)
myFoo.GetVector().push_back(itoa(i)); //This fills your vector.

std::string myString = getStringAtIndex(myFoo, 2);

//outside main

std::string getStringAtIndex(foo& fooToParse, unsigned int idx)
{
   return idx < fooToParse.GetVector().size() ? fooToParse.GetVector()[idx] : "ERROR!";
}

//If you prefer:


std::string getStringAtIndex(foo& fooToParse, unsigned int idx)
{
if(idx>fooToParse.GetVector().size())
return "ERROR!";
else
return fooToParse.GetVector()[idx] ;
   
Last edited on
I finally got it work. Sorry for the late response. Thank you all for your replies they have helped and given me some new ideas to implement.
Topic archived. No new replies allowed.