# neilski_2003

Member

138

142 Neutral

• Rank
Member
1. ## back to vectors

Oh and if i sounded ungrateful about Zahlmans help that was not my intent. Infact much the opposite. Whats the point in posting if you are not able to accept where you have gone wrong? After all theres a big difference between things working and things being efficient.
2. ## back to vectors

cheers guys. I thought that would have been the problem - i thought the question was a bit strange to begin with as from what i had read this would happen (i.e the iterator would be invalidated) Maybe i had i right in the first place. But as the other fella pointed out it seems a bit irish to do it that way as there are more direct routes of doing it. I guess they just want to get you using insert and resize so you know how, even if it isn't the most efficient way to do it. Thanks again Neil.
3. ## back to vectors

Hi guys, I posted a couple of days ago about using vectors and the insert function etc and got told that although what i was doing would work it was also extremely odd and basically wrong in many ways. So i went back to the problem to try and be more direct and do exactly what the question asked. Only problem is now i get a whole new error. And i'm not entirely sure why. Anyways here is the code that is causing all the problems. [source lang = "cpp"] vector<Student_info> extract_fails_insert(vector<Student_info>& students) { vector<Student_info>::size_type i = 0; // set up a variable to hold how many fails there have been vector<Student_info>::size_type passes = 0; //capture the size of the original vector for resizing later vector<Student_info>::size_type size = students.size(); vector<Student_info>::iterator iter = students.begin(); while (iter != students.end()) { if (fgrade(*iter)) { ++iter; } else { ++passes; students.insert(students.begin(), *iter); } } //adds the pass marks to the start of the student vector students.resize(passes); //returns the reduced student vector return students; } So basically what should be happening is this. I pass in a vector and then go through each element (using an iterator) to test whether it is a pass or a fail. If its a fail then it should just move on to the next element. If its a pass it should insert the record at the start of students and then continue. once it has made its way through students then it should resize it so that only the passes are left in the vector and then return this vector. Unfortunately when i compile that i get a "Vector Iterators Incompatible" error, and i have no idea why. It is obviously caused during the call to the insert function but i am not sure how i could sort this out. Maybe i could copy the current bit of info into a local variable and then place this into the insert function rather than using *iter. I don't know. So if anyone can shed any light on this it would be most appreicated. Cheers Neil
4. ## testing for existance of a character

I agree with you as an ideal situation. And its not that i won't learn this - everything i do in c++ is documented on paper as well as the practical exercises and I have a page dedicated to new ideas i must revisit. Its just that my knowledge is such that only entirely structured learning - e.g folowing through this book - is going to give me a sound enough base from which to develop. After all its no use having advanced ideas on how to solve one thing if you have no ideas on how to solve another. Once this book has finished I will be straight back through the ideas on my list and trying to apply them to the relevant exercises etc, and hopefully then i will have a better understanding of why i've done it and how it works - not just because someone told me to do it on a forum. This works best for me - and although it might be roundabout it tends to get me results in the long run. Cheers for all your input. Neil
5. ## testing for existance of a character

well that answered my question (before i asked it). Sorry guess never seen that find_first_off stuff - possibly why i am in the beginners forum. So i'll stick without it for now until such times as it comes up in the book or i have time to find out about it. Cheers. Neil
6. ## testing for existance of a character

thats what i though but if i use the == sign i get two errors about not being able to convert chars to ints. What do i need to do to chage that? Cheers Neil
7. ## iterators - whats this doing?

if i wanted to insert the pass records straight into the start of students, without using a locally created vector how would i do that?. I did it that way because i have to use the insert function and as far as i am aware that doesn't take indexes in the range fields. what i really want to do is basically say if its a pass copy to the start of students, if its a fail go to the next one. Then at the end resize the vector to get rid of all the fails using the resize function. The use of resize and insert are specified by the question in the book i am going through "Accelerated C++".
8. ## testing for existance of a character

hi guys i am trying to check a word to see if it has a particular character in it and this is what i ahev come up with [source lang = "cpp"] vector<string> ascdsc (vector<string> & words) { string::size_type j = 0; string::size_type i = 0; for (j = 0; j != words.size(); ++j) { cout<<words[j]<<endl; for (i = 0; i != words[j].size(); ++i) { cout<<(words[j])<<endl; if ((words[j]) = "b" && "d" && "f" && "h" && "k" && "l" && "t" && "g" && "j" && "p" && "q" && "y") { cout<<"Word has a asc/dsc in it"<<endl; } else { cout<<"you don't smell"<<endl; } } } return words; } only problem is - doing it this way reckons that every character is one of those i have entered. Can anyone tell me what i am doing wrong. I have tried putting == in the if statement but that just creates an error. Any help would be appreciated. Neil
9. ## iterators - whats this doing?

Hi all, this bit of code seems to work but i want to make sure that i understand what i have done. So here is the code snippet and i will say below what i reckon it is doing. [source lang = "cpp"] vector<Student_info> extract_fails_insert(vector<Student_info>& students) { vector<Student_info>::size_type i = 0; vector<Student_info>::size_type fails = 0; vector<Student_info>::size_type size = students.size(); vector<Student_info>::iterator iter = students.begin(); vector<Student_info> passes; while (iter != students.end()) { if (fgrade(*iter)) { ++fails; ++iter; } else { passes.push_back(*iter); ++iter; } } students.insert(students.begin(), passes.begin(), passes.end()); students.resize(size - fails); cout<<students.size()<<endl; return students; } Basically i am reckoning that this cycles through the students vector that was passed into it. If it fails it adds to the counter which is at the end used to modify the size of students. If it passes it adds the record to the locally created passes vector and moves onto the next element. Once the end is reached it inserts the contents of the passes vector onto the beginning of the students vector. It then resizes this vector by removing the amount of fails from the original size of the vector. Is this assumption correct. The reason i ask is that it seems to be working fine in my code but i'm not 100 % sure that its correct. Cheers for any help. Neil
10. ## Looking for a 'Mentor'

Hi all, well if you've got this far i guess the title didn't put you off. And its actually probabaly not as bad as it seems anyway. Its not exactly mentorship i am after but more accurately a single source. Obviously different coders have their own style and sometimes this is problematic when trying to solve problems because you get 4 or 5 possible ways to do things and you don't really take any of them in. So i was wondering if anyone would be up for helping me with any problems i may have trying to complete the exercises in accelerated c++. The only reason i ask is so that i come out of the book with a consitent style rather than one that is a mish mash of everyones ideas and therefore not something i can easily reference again. Obviously the normal rules of engagement apply - i won't just ask you to solve the exercises but will try them myself first and only if they don;t work will try and get some help. Cheers in adavance for any help offered. Neil.
11. ## Vector Issues - What am i doing wrong?

Yeah someone said that too me on here and recommended it because it would actually be useful. I did some c++ at uni but it was very much the 'learn whats needed to pass the unit' type of learning so i needed something to fill in the rest so i actually know what i am doing. I've found the exercises brilliant to be honest. Challenging but not impossible, and they also make you use the things they have introduced in the chapter to the extent that they become 'almost' second nature. Muchos Kudos to the person(s) who recommended it to me. Whomever they may be! Cheers Neil
12. ## Vector Issues - What am i doing wrong?

Saruman, Nope its from "Accelerated C++" but i'd imagine a liot of the books have similar exercises as they should all cover much the same stuff. To be precise its exercise 3-3. Cheers Neil
13. ## Vector Issues - What am i doing wrong?

I missed your first post about the cin bit, but having reviewed it and had a look i have managed to remedy my error. Thanks to you two my program actaully does what its supposed to do. Cheers The new code is here [source lang = "cpp"] //write a program to count how many times each distinct word appears in its input #include <iostream> #include <algorithm> #include <iomanip> #include <string> #include <vector> using namespace std; int main() { cout<<"Please Enter the Text You Wish to be analysed one word at a time :"<<endl; vector<string> words; string text; while (cin>>text) { words.push_back(text); } typedef vector<string>::size_type vec_sz; vec_sz size = words.size(); //check that a word was entered if not wuit the program if (size==0) { cout<<endl<<"You Must Enter atleast one word."<<endl; return 1; } sort (words.begin(), words.end()); int counter; counter = 0; // create a loop to go through each item in the vector for (vec_sz i = 0; i<size; i++) { //compare the current index with the next - if they are not the same add to counter cout<<i<<endl; //if your not at the last item and the words are different add to the counter if (i+1 < size && words != words[i+1]) { ++counter; } cout<<words<<endl; } if (words[size -1] != words[size-2]) { ++counter; } cout<<"There are "<<counter<<" different words in that text"<<endl; system("PAUSE"); return 0; }
14. ## Vector Issues - What am i doing wrong?

Thanks for that, I'm at chapter 3 in this book as you could probabaly tell from the schoolboy nature of my error. Still can't figure out what i am doing wrong with regards to the first element dissapearing but what can you do?
15. ## Vector Issues - What am i doing wrong?

Thanks for that, I shall try and fix the inner loop now and see what happens. Come a cropper probably wasn't the right phrase but it basically means that i've tried to do something that i couldn't and been caught out.