Iterator basic doubt

Hello, I'm new at C++ andI cannot understand some things of this function. My questions are:

1. In the "for" structure, which is just at the end of the function:
1.1. How can you iterate with a string? For me it makes no sense...
1.2. why is the value of the boolean find assigned to an iterator? You assign "d" true or false?
1.3. What does AllNames.begin()? Does it refer to the first name ("(Ni)")that appears in the AllNames array defined previously?

I would appreciate a lot your help,

Ramon
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
  vector<double> GetDiffusivities(double T, Cantera::thermo_t &Phase)
{
    /*Parameters definition*/
    /*Nickel surface*/
    double EHNi  (14.65e6), ENi         (93.2e6);       /*J/kmol*/
    //double ECONi (28.87e6), ECO2Ni      (28.87e6);      /*Zhu et al.*/
    double ECONi (11.30e7), ECO2Ni      (11.30e7);      /*Hanna et al*/
    
    double DHNi  (2.5e-7*exp(-EHNi/(Cantera::GasConstant*T)));
    double DNi   (1.4e-6*exp(-ENi/(Cantera::GasConstant*T)));

    double DCONi (1.2e-9*exp(-ECONi/(Cantera::GasConstant*T)));
    double DCO2Ni(1.2e-9*exp(-ECO2Ni/(Cantera::GasConstant*T)));
    /*YSZ surface*/
    double EOX(49e6);                                   /*J/kmol*/
    
    double DOX   (5.846e-15*exp(-EOX/(Cantera::GasConstant*T)));
    double DCOX  (1.9e-11);
    double DZr   (5e-8);
    double DHZr  (5e-8);
    
    vector<double> Diffusivities(Phase.nSpecies(),0.0);
    vector<string> AllNames
    {   "(Ni)","H(Ni)","O(Ni)","O'(Ni)","OH(Ni)","H2O(Ni)",
        "CO(Ni)","CO2(Ni)",
        "(X)","O(X)","O'(X)","O''(X)","OH'(X)",
        "CO(X)","(Zr)","H'(Zr)","OH'(Zr)"};
    vector<double> AllDiffusivities
    {   DNi, DHNi, DNi, DNi, DNi, DNi,
        DCONi, DCO2Ni,
        DOX, DOX, DOX, DOX, DOX, DOX,
        DCOX, DZr, DHZr, DZr};
    
    bool find(false);
    unsigned int s(0), n(0);
    for(s=0; s < Phase.nSpecies(); s++)
    {
        std::vector<string>::iterator D = std::find(AllNames.begin(), AllNames.end(), Phase.speciesName(s));
        Diffusivities[s] = AllDiffusivities[D-AllNames.begin()];
//cout<<*D<<"\t"<<D-AllNames.begin()<<"\t"<<Diffusivities[s]<<endl;
    }
    return Diffusivities;
}
How can you iterate with a string?
You don't. On line 38 iterator which can point to vector of string element (i.e. string) is declared and defined.
Think about iterator like of pumped up pointer.
why is the value of the boolean find assigned to an iterator?
Why do you think it is a boolean?
http://en.cppreference.com/w/cpp/algorithm/find
There is no function called find which returns boolean. It returns iterator (pointer) to found element or to the end (past-the-end for searched range) iterator if not found.
What does AllNames.begin()
It returns an iterator to the first element of the vector. end() function will return one-past-the-end iterator.
If array values[10] was a class then begin() would return (values) pointer, and end()(values + 10)
Last edited on
Thanks for your response. These days I've tried to understand all you told me and the problem I have is that I don't understand why is necessary to use pointers. I mean , why don't you refer directly to a variable instead of refering to the address of this variable. I don't see the point in it. Maybe it's because I don't understand pretty well pointers or iterators

Pointers can change dynamically to refer to different objects in memory. Variables always name the same object.
Pointers:
1) Pointers (or preferably smart pointers) are used to point to heap variables (which does not have names per se) which we have to use if we don't know in advance how many/which size of variables we need.
2) Pointers can be used to point to ane data and when passed into function change that data (references — which usually internally a pointers — are more preferable in C++ but you cannot pass a null reference for example)
3) Pointers can be used as iterators for c-arrays

Iterators:
1) Used to generalise your code (usually with a help from templates): You should not care if those iterators are pointing to vector elements, string characters or list nodes, you would use them exactly same way.
2) Not every container type allows index based access. How would you iterate over a list without using iterators? Or a map?
So then, expecifically on this line of the code (on line 38):

std::vector<string>::iterator D = std::find(AllNames.begin(), AllNames.end(), Phase.speciesName(s));

what does exactly happen? I use the funtion "find" in order to find the first string between AllNames.begin() and AllNames.end() that is like Phase.speciesName(s).
Once done this, it passes the address of the string founded to a position of a vector of strings?

I'm sorry if for you this is too basic, but maybe with this example I'll be able to see it clearly..!
std::find returns an iterator. The return type of std::find is the same as the type of D.
Topic archived. No new replies allowed.