Refrences parameters question

Just wanting know for something like
 
  vector <string> split(const string &str)


does &str refrence to the vector named split or what? Here is the full code.
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
#include <iostream>

#include <vector>

using namespace std;
bool not_space (char c){

return !isspace(c);
}
bool space(char c){

return isspace(c);
}
vector <string> split(const string &str){
typedef string::const_iterator iter;
vector <string> ret;

iter i = str.begin();

while (i!= str.end()){


    i = find_if (i,str.end(), not_space);
    iter j = find_if(i, str.end(),space);

    if (i != str.end())
        ret.push_back(string(i,j));
    i = j;
}
return ret;

vector <string> split(const string &str) declares a function
named split, which
takes constant reference to string as argument and
returns a vector of strings
So it does mainiuplates a vector right?
It creates a vector and returns it by value.
so &str becomes a vector? or it just a reference to whatever is passed onto vector <string> split(const string &str) I don't understand so is str refrenceing to split or a refrence to whatever is passed into str?
const string& str is a reference to original string which is passed into function. It is made a reference to eliminate cost of copying string int function.

Inside function a vector of string is created which is filled by copies of parts of original string ant then returned for the function caller to use. Original string is not changed (hence const)
So, Vector <string> split is part of str? what is the point in calling vector in a function anyways? So when the book calls the iter i into str.begin does that mean it is now the value of the vector<string> split? Ok I understand the reference copying the value thats going to be passed into a function. And we call it const so we won't change the value inside str ,but explain why the function is a vector <string> split is a vector string split does this

1
2
3
typedef string::const_iterator iter;


manipulates Split and then (I) gets set equaled to str.begin() which is then refrencing into what ever value gets passed into it?
So, Vector <string> split is part of str?
No. split is a function name. Which you use to call function.
You give this function a single string.
It looks at it and gives you a vector of strings.

str is just how given string is called internally in function (you have to name it to be able to access it)
Ohhh ok, I get it so how would you be able to call it in main()?

Thanks for explaining to me yeah I get it now.
Topic archived. No new replies allowed.