symbol "->"

Can anyone explain I Did not understand the symbol "->".

1
2
3
4
5
it=mylist.begin();            
if (it‐>word == mystr.word)     // if word of it and mystr is the same         
it‐>frequency ++;               // add 1 to the frequency value  
else        
mylist.push_back(mystr);  // push mystr at the back of mylist    
Last edited on
Do you know the dot operator? its the equivalent for pointers.
Like this shadowCODE ::?
1
2
3
4
5
it=mylist.begin();            
if (it::word == mystr.word)     // if word of it and mystr is the same         
it::frequency ++;               // add 1 to the frequency value  
else        
mylist.push_back(mystr);  // push mystr at the back of mylist  
I'll give you an example see if that helps.

If you have a class.

1
2
3
4
5
6
7
8
9
class Tester
{
public:
	Tester();
	~Tester(){};

	void testing(){ cout << "Hello << endl; }
       
}; 


Back in main, if you wanted to call that function inside the class, you would have to create an object, and use the dot operator.

1
2
Tester test;
test.testing();


Thats how you would do it normally. But if "test" were to be a pointer, which it is when you allocate memory for example

Tester* test = new Tester;

now test is a pointer, and test.testing(); does not work anymore. You will have to do test->testing();
Last edited on
If you want to acces your function inside some other class with pointer you have to first make a pointer and set it to your object. Now with a adress of your object you can chosse function from class. Like this: pointer(has addres of your object)->func.
Lee:

If you understand structs/classes, the basic idea is that you access members of a struct class through a specific object by using the dot (.) operator:

1
2
3
4
5
6
7
8
9
10
struct Example  // a struct with 2 members, 'foo' and 'bar'
{
    int foo;
    int bar;
};

// now, each 'object' of that struct has its own foo and bar members:
Example obj;  // 'obj' is our object
obj.foo = 5;  // set obj's foo member to 5
obj.bar = 10;



The -> operator does the same thing, but instead of working with an object, it works with a pointer to an object:

1
2
3
4
Example* ptr = &obj;  // ptr points to our obj object

ptr->foo = 10;  // access 'obj's foo member
(*ptr).foo = 10; // <- alternative way to do the exact same thing 



The difference being:

dot (.) works on the object to the left
arrow (->) works on the object being pointed/referred to by the object to the left



In your example with iterators, 'it' refers to an element in your 'mystr' container.

1
2
3
4
5
6
it‐>frequency ++;  // <- change the 'frequency' member of whatever object 'it' is referring to

// vs.

it.frequency++; // <- changes 'it's frequency member (which would be an error because
  // iterators probably don't have a 'frequency' member). 




EDIT:

Ninja'd x2
Last edited on
I try to understand about it but it's make my program error especially in line 44.
the error is at the itterator with symbol ->.
I try the best but I spend a lot of time to understand it.


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
#include<list>
#include<string>
#include <string.h>

using namespace std;

bool wordExist(list<string> thislist, char *pch);
list<string>::iterator determinePosition (list<string> &xlist, char *p);

struct infoType{
	string word;
	int frequency;
} info;


int main()
{
	char str[300] , *pch;
	list<string> mylist;
	list<string>::iterator it;
	
	string mystr;
	
	info.frequency=1;
	info.word="school";
	

	
	cout<<"Please enter your passage:\n";
	cin.get(str,300);
	
	pch= strtok (str, " ,.-");
	
	while(pch!=NULL)
	{
		if(!wordExist(mylist,pch))
		{
			it=determinePosition(mylist,pch);
		
			mylist.insert(it,pch);
		}
			it=mylist.begin();
		if(it->info.word==mystr.word)
		{
			it->frequency ++;
		}
		else
			mylist.push_back(mystr);
			
		pch=strtok(NULL," ,.-");
	}
	
	cout<<endl<<"The passage contains: \n";

	for(it=mylist.begin() ; it!=mylist.end() ; ++it)
		cout<<endl<<*it;
	cout<<endl;
 
	return 0;	
}


	bool wordExist(list<string> mylist, char *pch)
	{
		list<string>::iterator it;
		
		for(it=mylist.begin() ; it !=mylist.end() ; ++it)
			if(*it==pch)
				return true;
			return false;
	}
	
	
	list<string>::iterator determinePosition (list<string> & xlist, char *p)
	{
		list<string>::iterator it;
		
		for(it=xlist.begin() ; it!=xlist.end() ; ++it)
			if(*it > p)
				return it;
			return it;
	}
1
2
3
4
	list<string> mylist;
	list<string>::iterator it;
//...
			it->frequency ++;


mylist is a list containting strings.

'it' is an iterator that refers to a string in that list

strings do not have a 'frequency' member.



The frequency member is part of your 'infoType' struct ... so you probably want a list<infoType> and not a list<string>.
Help me I still did not understand about itterator "->".
Need your help.
closed account (EwCjE3v7)
iter->empty() is the same as (*iter).empty()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    vector<string> vs;
    string s;

    while (cin >> s) {
        vs.push_back(s);
    }


    for (vector<string>::const_iterator iter = vs.cbegin(); iter != vs.cend() && !iter->empty(); ++iter) {
    //                                                                                 ^ same as !(*iter).empty()

        cout << *iter << " ";
    }
    cout << endl;

    return 0;
}


The code above that you provided

1
2
3
4
5
it=mylist.begin();            
if (it‐>word == mystr.word)     // if word of it and mystr is the same         
it‐>frequency ++;               // add 1 to the frequency value  
else        
mylist.push_back(mystr);  // push mystr at the back of mylist   


can be written as

1
2
3
4
5
it=mylist.begin();            
if ((*it).word == mystr.word)     // if word of it and mystr is the same         
(*it).frequency++;               // add 1 to the frequency value  
else        
mylist.push_back(mystr);  // push mystr at the back of mylist   
Last edited on
Help me I still did not understand about itterator "->".
Need your help.


Look at what you're doing here:

1
2
3
4
	list<string> mylist;
	list<string>::iterator it;
//...
			it->frequency ++;


mylist is a list<string>. Note that the part in the <angle brackets> is 'string'. This means that each element inside the list is a string.

When you have an iterator, it refers to a single element inside that list. In your case, this means an iterator refers to a string.

The -> operator is like saying "Okay, this iterator is referring to a string, and I want to do something with that string." In your case, you are doing this:

it->frequency ++;

So you're saying "I want to increase that string's frequency by 1".



Make sense so far?


All of that is fine and good... but your problem (and the thing that is giving you the error) is that strings do not have a frequency, so what you're trying to do doesn't make any sense.

Your frequency variable is not inside the string, but is inside your 'infoType' struct. Therefore, your list is wrong. You want a list of infoTypes, not a list of strings:

 
list<infoType> mylist;  //<- a list of infoTypes 
I didn't get this symbol also
Topic archived. No new replies allowed.