read in from file

Im trying to find a quicker way to do this:
1
2
3
4
5
6
7
8
9
string line;
  ifstream myfile (dictFile);
  if (myfile.is_open()){
	while(getline(myfile, line)){
	  data newWord(line);
	  origDict.push_back(newWord);
	}
   myfile.close();
  }


where in my .h file i have a vector<data> origDict and struct data that contains string word and char[26]letters.

Iv searched almsot everywhere but cant find anything that accomplishes this task. Any ideas?
What is the code in the data constructor?
1
2
3
4
5
6
7
8
data(const string &w){
  word=w;
  for(int i=0; i<26; ++i)
	letters[i]=0;
  for(int i=word.length()-1; i>-1; --i){
	letters[word[i]-'a']+=1; 
  }
}
Craptastic timeout!!!!

Here's the original message I just tried to post:

Ah. Give a try to see if this is any faster:

1
2
3
4
5
data(const string &w): letters{0}, word(w) {
  for (char& c : w){
    letters[c-'a']+=1;
  }
}

If you aren't using C++11 you'll have to change a couple of things:

1
2
3
4
5
data(const string &w): word(w) {
  fill( letters, letters + 26, 0 );
  for (string::const_iterator c = w.begin(); c != w.end(); ++c){
    letters[*c-'a']+=1;
  }

I presume you know that this is dangerous. You should really check that c is valid:

4
5
    unsigned n = *c-'a';
    letters[n<26 ? n : 0]+=1;

Hope this helps.
thanks i gave it a go and your code didn't really change much. That doesn't mean that it isn't faster because Iv done some other things to it to speed it up that just didn't even thou I know that should. I think its how the rest of my program is coded that is causing this.
However, I'v done some additional tweaking and its running at a speed that I can be half way satisfied with, especially for the level of my coding expertise. Thanks again for your input I really appreciate it.
Topic archived. No new replies allowed.