Thread: Problems with sorting

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Problems with sorting

    I am trying to sort these 2 lines that I have in a .txt file:

    ABC,3
    DEF,2


    I want it to sort it in Ascending order so it will look like this:

    DEF,2
    ABC,3


    With the code below it is doing a sort that look like this(with a blanc space also ?):

    ABC,2

    DEF,3


    One problem that I have is that the first thing I am reading in is a string and the second an int. So I am using 2 different vectors for these, (string and int).
    This is why the output look like this. It is logic for me.

    So what do I have to add or change in the code to make it look like:
    DEF,2
    ABC,3


    (The sorting will be made on the integers in ascending order)
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    #include <vector>
    #include <cmath>
    #include <algorithm>
    
    using namespace std;
    int main () 
    { 
    	
    	
    	char Comma;
    	std::string Symbol;
    	int Action = 0;
    	int Date1 = 0;
    	int Date2 = 0;
    
    	int Number = 0;
    	int t = 0;
    
    	std::vector<std::vector<int> > Values(3, std::vector<int>(10));
    	std::vector<std::vector<string> > Values2(1000, std::vector<string>(10));
    
    	ofstream Sort1;
      
    	Sort1.open ("Sort1.txt");
    	ifstream Sort ("Sort.txt");
    
    	while	(    getline(Sort, Symbol, ',')   )           		
    	{
    		t = (t + 1);		// Count
    		
    		Sort >> Action;		// Buy
    		{
    			Values2[t][1] = Symbol;
    			Values[t][2] = Action;
    			
    		}
    	}
    
    	std::sort(Values.begin(), Values.end());
    
    		for (int Rowsen = 1; Rowsen < (t + 1); Rowsen++)			//Number of Lines
    
    		{
    			Sort1 << 
    			Values2[Rowsen][1] << ',' <<
    			Values[Rowsen][2]
    			<< "\n";
    		}
    
    		return 0;
    }
    Last edited by Coding; 01-21-2008 at 04:38 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably want to store your data as a struct, rather than as two individual arrays, and you will automatically solve the problems.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I have never understand how struct works. That is really new to me.

    I had an explanation on this but did never really understand the logic of this.
    Do you know any example/approach how this could be added to this examplecode...
    Last edited by Coding; 01-21-2008 at 04:45 PM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, it's not really my place to write the code for you, but you could have a struct like this:
    Code:
    struct value {
       int action;
       string symbol;
    };
    Then you create a vector of "value", and store a "value" entry for each data-item you have.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I am not sure if I understand.
    This declaration with the struct, is this happening before or after you get the values from the file and the vector that I will create to store these values, this must still be 2 vectors since one vector can&#180;t contain both int and string at the same time ?
    I am really lost on this one.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The struct declaration shouldn't be in your main function at all; either in a header file, or at the top of the .c file if you're only using a single source file.

    A vector must be of only one type, but that type can be user-defined. In this case, we want a vector of type value.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Code:
    struct value {
       int action;
       string symbol;
    };
    So where in the code should this be if not in the main function or above the int main ()?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Coding View Post
    Code:
    struct value {
       int action;
       string symbol;
    };
    So where in the code should this be if not in the main function or above this ?
    I would recommend between all the lines that say #include and the line that says int main, if you're not using any .h files of your own. So above main (it needs to be there before you try to use any variables of that type).

  9. #9
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25
    yes the struct is difficult to learn

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I understand.
    Does this meen that I can declare a vector that look like this as I first have declared a struct that is named "Value"
    inside int main() ?
    Code:
    std::vector<std::vector<Value> > Values(3, std::vector<Value>(10));
    When I refer to action and symbol later in the code. Can I put these to this same vector then ?
    Has these int and string variables been declared as something else, like a Value ?
    Last edited by Coding; 01-21-2008 at 06:32 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    We've got a tutorial on structures on the main site, if you're interested.

    The syntax is not quite right, I think. You're trying to use 3 as size, and std::vector<Value>(10) as the value for the initializer? That initializer might work, but I wouldn't bother. You can just leave it blank; you'll get uninitialized data to start with, but if you plan to read in each element that won't matter. And you only need one vector:
    Code:
    std::vector<Value> Values;
    is a currently empty vector of things of Value type. As you read things in, you can use push_back to add them to the vector. This way you don't have to know how many things are in your file ahead of time.

    You access members of structs with dots: a_Value_variable.action. In your case Values is a vector of Value, so Values[0] would be the first Value item, which means you can do Values[0].action and Values[0].symbol.
    Last edited by tabstop; 01-21-2008 at 06:41 PM. Reason: lost in the syntax

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Tabstop,

    Thats a good explanation, I think I am close to the solution.
    I have tried this code out with all the declarations. But the compiler says that &#180;Symbol&#180; : undeclared identifier.
    I beleive I have declared it after the # include.
    I will also check the tutorials out..

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    #include <vector>
    #include <cmath>
    #include <algorithm>
    
    struct Value 
    	{
    		int Action;
    		std::string Symbol;
    	};
    
    using namespace std;
    int main () 
    { 
    	
    	char Comma;
    	int t = 0;
    	std::vector<Value> Values (3);
    
    
    	ofstream Sort1;
      
    	Sort1.open ("Sort1.txt");
    	ifstream Sort ("Sort.txt");
    
    	while	(    getline(Sort, Symbol, ',')   )           		
    	{
    		t = (t + 1);		// Count
    		
    		Sort >> Action;		// Buy
    		
    			Values[t] = Symbol;
    			Values[t] = Action;
    			
    		
    	}
    
    	std::sort(Values.begin(), Values.end());
    
    		for (int Rowsen = 1; Rowsen < (t + 1); Rowsen++)			//Number of Lines
    
    		{
    			Sort1 << 
    			Values[Rowsen] << ',' <<
    			Values[Rowsen]
    			<< "\n";
    		}
    
    		return 0;
    }
    Last edited by Coding; 01-21-2008 at 06:54 PM.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Symbol is only defined in the context of a Value struct. Values[t] -- your indexing is off here, by the way, but that's a sidebar -- is a Value hence Values[t].Symbol is defined. Symbol by itself is not.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I beleive I have to check out the tutorials. There is somthing I cant understand with the logic. I think I am near to understand but something is missing...

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That means roughly what it says: if you want to initialize Action2 in that manner, then it must be a static constant -- which means that (1) each Value would share the same copy of it[1] and (2) its value could never change.

    This isn't what you want, so don't do that. (You don't need to initialize it anyway, since you shouldn't use it until you read in a value from your file.)

    [1]I think. That's what would happen in a class, anyway; I don't know whether it happens that way in a struct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problems.
    By Sedvan in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2008, 01:13 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. Sorting structures
    By RedRum in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2002, 12:19 PM