Thread: Questions about tasks! And view of code, please ^^

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    Question Questions about tasks! And view code, please ^^

    Well, this code is the first code I've actually made on my own, by my own thinking and that's kinda advanced and loads of thinking (For me!) so it's an achievement for me ^^ Dunno if you peeps remember the feeling and maybe the code that was your first very own, working program :P anyways here's the code:

    Code:
    #include <string>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using std::string;	using std::cout;
    using std::cin;		using std::vector;
    using std::endl;	using std::sort;
    
    int main(){
    
    	cout<<"Please input a sentence: ";
    
    	vector<string> inpVecStr;
    	string input;
    	while(cin>> input)
    		inpVecStr.push_back(input);
    	//Now the inputted sentence is in inpVecStr
    
    	vector<string> indexedVec;
    
    	{string inputtedStr;
    	for(vector<string>::size_type i = 0; i != inpVecStr.size(); ++i)
    		inputtedStr += inpVecStr[i] + " ";
    	inputtedStr += "\n";
    	indexedVec.push_back(inputtedStr);
    	} //inputtedStr is deleted here
    
    	for(vector<string>::size_type i = 0; i != inpVecStr.size() - 1; ++i){
    
    		vector<string>::const_iterator it = inpVecStr.begin();
    		inpVecStr.push_back(*it);
    		inpVecStr.erase(inpVecStr.begin());
    		
    		string tmpString;
    		for(vector<string>::size_type j = 0; j != inpVecStr.size(); ++j)
    			tmpString += inpVecStr[j] + " ";
    		tmpString += "\n";
    
    		indexedVec.push_back(tmpString);
    	}
    
    	for(vector<string>::size_type i = 0; i != indexedVec.size(); ++i)
    		cout<< indexedVec[i];
    
    	
    	return 0;
    }
    Well, I'd like you peeps to check for stuff that could be improved, if you have the time, just so that I can learn of it, also I'm not very good at using comments, sorry, but they might be wrong, as in, I typed one in there 'cause I think it works like that, so if it doesn't please correct me :P (it's the one, //inputtedStr is deleted here)
    Also, I have some questions about my task :P
    Design and implement a program to produce a permuted index. A what? :S
    2. Sort the rotations.
    3. Unrotate and write the permuted index, which involves finding the separator, putting
    the phrase back together, and writing it properly formatted.
    What am I supposed to sort o.O? and the 3rd just makes no sense :P
    Sorry I ain't English :P
    Last edited by Akkernight; 12-18-2008 at 12:57 PM.
    Currently research OpenGL

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    while(cin>> input)
    		inpVecStr.push_back(input);
    should be replaced with
    Code:
    std::getline(std::cin,input);
    Code:
    vector<string>::const_iterator it = inpVecStr.begin();
    		inpVecStr.push_back(*it);
    		inpVecStr.erase(inpVecStr.begin());
    What are you trying to do there?

    You are creating an iterator to the beginning element(which if I recall correctly is not a valid index until incremented), and then you push it onto the vector, then you erase that same element from the vector.
    Last edited by Raigne; 12-18-2008 at 01:08 PM.

  3. #3
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I take the first word in the vector, copy it to the end, then erase the first one :P and I just used iterator to learn using them :P And it does work!
    And if I use getline, how can the program then work o.O? I need every word seperated
    Last edited by Akkernight; 12-18-2008 at 02:01 PM.
    Currently research OpenGL

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Design and implement a program to produce a permuted index. A what? :S
    2. Sort the rotations.
    3. Unrotate and write the permuted index, which involves finding the separator, putting
    the phrase back together, and writing it properly formatted. What am I supposed to sort o.O? and the 3rd just makes no sense :P
    Well, I didn't compile it and can't tell easily what exactly is the purpose of what you are doing.

    Since you seem to take much care to use the standard library stuff (like vector and string), you should also familiarize yourself with another important part of it: the algorithms.

    In particular you could be interested in algorithms like next_permutation (?), prev_permutation (?), rotate and sort. Generally it is not wise to reinvent existing algorithms (unless it is for learning purposes).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Getline allows you to take a entire string until a '\n' newline is encountered. So, what you could do is input a string like "A B C D E F G", and then tokenize it to seperate the individual words. Would be a good practice to get use to manipulating strings, and tokenizing is very helpful in many programming aspects.
    Last edited by Raigne; 12-18-2008 at 02:54 PM.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It's really up to the OP to decide which he wants to use. You should realize that cin >> already does tokenize the input exactly as you suggest (except you can't tell which words were on the same line, should you need that).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I've only learnt to use the std::sort from algorithm :P and I'm trying to make a permuted index... and now it's output is...
    I input: The quick brown fox
    it outputs: The quick brown fox
    quick brown fox The
    brown fox The quick
    fox The quick brown
    Currently research OpenGL

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Dumb Question: When I run that software unchanged. I have to put the string i want to input, and then input exit for the rest of the software to run? Although i dont really know why. Also, if I input "exit" before any other strings it causes a dereference error in the vector(I expected that).

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, the point of the input loop is that input is terminated when EOF is encountered (Ctrl+Z on Windows). Works also nicely if stdin is redirected to a file.

    Below is what a program that does exactly the same would look like when you take advantage of what the standard libraries offer.

    Code:
    #include <string>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    //I actually don't see much point in doing this for things that you are going to use once.
    //just typing std::sort once or twice in the code is a lot simpler
    using std::string;	using std::cout;
    using std::cin;		using std::vector;
    using std::endl;	using std::sort;
    
    //it is useful to separate code with a specific task into a separate function for ease of reuse
    //a ready-made function by this name also exists in the Boost.StringAlgorithm library
    string join(const vector<string>& source, const std::string& delimiter)
    {
        string concatenated;
        if (!source.empty()) {
            concatenated = source[0];
            for (vector<string>::size_type i = 1; i != source.size(); ++i) {
                concatenated += delimiter + source[i];
            }
        }
        return concatenated;
    }
    
    
    int main()
    {
        cout<<"Please input a sentence: ";
    
        vector<string> inpVecStr;
        string input;
        while(cin>> input)
            inpVecStr.push_back(input);
    
        vector<string> indexedVec;
    
    
        if (!inpVecStr.empty()) {
            //store the first unrotated "sentence"
            indexedVec.push_back(join(inpVecStr, " "));
    
            //rotate input.size() - 1 times (rotating one more time would produce the original input)
        	for (vector<string>::size_type i = 1; i < inpVecStr.size(); ++i)
        	{
                //unless for learning purposes, always prefer a suitable STL algorithm over hand-rolled loop
                //this rotates everything by one position to the left
                std::rotate(inpVecStr.begin(), inpVecStr.begin() + 1, inpVecStr.end());
                indexedVec.push_back(join(inpVecStr, " "));
            }
    
            //print all rotations :)
            std::copy(indexedVec.begin(), indexedVec.end(), std::ostream_iterator<string>(cout, "\n"));
        }
    
        return 0;
    }
    Last edited by anon; 12-18-2008 at 04:52 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ok, I surely learnt from that, but no one has answered teh questions yet xD

    Design and implement a program to produce a permuted index. A what? :S
    2. Sort the rotations.
    3. Unrotate and write the permuted index, which involves finding the separator, putting
    the phrase back together, and writing it properly formatted. What am I supposed to sort o.O? and the 3rd just makes no sense :P

    That
    And there is maybe another thing I should show; the result
    But I can't use loads of spaces :/ but it's a permuted index...
    Currently research OpenGL

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You probably need to start by finding out what a "permuted index" is

    Then you think how you'd go about it (possibly follow some links to see if there are descriptions of algorithms that are used to implement one which I can't understand whether you have done ).

    From the looks of the example in Wikipedia you might indeed need to get the input as lines.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    KWIC o.O? Ok... I think I'll just skip those two tasks :P
    Currently research OpenGL

Popular pages Recent additions subscribe to a feed