Thread: Struct's

  1. #16
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, you have a set with strings where strings are considered unique based on the 12 characters starting from the 259th character. You are now reading in a file containing 12 character long strings one at a time from said file and wish to remove any strings from the set where those 12 characters starting from the 259th character match the 12 characters starting from the 1st character of the new string you just read in from the file correct?

    I thought about this awhile and for the moment at least I was able to come up with something. Consider the following test program, you should focus on the parts in blue. Don't worry about the copy function if that confuses you:

    Code:
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <functional>
    
    using namespace std;
    
    struct Search : unary_function<string, bool>
    {
        string value;
        Search(const string& val) : value (val) {}
        bool operator()(const string& a)
        {
            return a.compare(4,3,value,0,3) == 0;
        }
    };
    
    struct compare: binary_function<string, string, bool> {
        bool operator()(const string& a, const string& b)
        {
            return a.compare(4,3,b,4,3) < 0;
        }
    };
    
    
    int main()
    {
        set<string,compare> strSet;
        set<string,compare>::iterator it;
    
        // Do some inserts into our set
        strSet.insert("Hello World");
        strSet.insert("Jello World");  // This one should fail, tests compare part
        strSet.insert("Kello Korld");
        strSet.insert("Me Happy Now");
    
        // Show original set
        copy(strSet.begin(),strSet.end(),ostream_iterator<string>(cout,"\n"));
    
        // Find first "app" in strings within set starting at 5th character
        it = find_if(strSet.begin(),strSet.end(),Search("app"));
    
        // If found, erase it from the set
        if( it != strSet.end() )
        {
            cout << "\nFound it: " << *it << '\n' << endl;
            strSet.erase(it);
        }
        else
            cout << "\nNot Found!\n" << endl;
    
        // Show set minus any "app" match that we found
        copy(strSet.begin(),strSet.end(),ostream_iterator<string>(cout,"\n"));
    
        return 0;
    }
    This test program considers strings to be unique going by the 3 characters starting from the 5th character in the string. It then uses the find_if template function (defined in the algorithm header) along with a unary predicate object that I called Search. This finds any matches amongst the strings stored in the set (starting at the 5th character and going for 3 character in length) with a control string (starting at the 1st character and going for 3 characters in length).

    And now the output:
    Code:
    Me Happy Now
    Kello Korld
    Hello World
    
    Found it: Me Happy Now
    
    Kello Korld
    Hello World
    That is very close to what you would need to do. Of course you need a loop since you are reading in a bunch of strings from a file, and your offsets and lengths used are going to be different, and you won't be using a hardcoded "app"... etc. etc. etc. Anyway, given the time I spent that's what I came up with.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Thank u for your help, but that I exactly want is this: imagine that I have a struct like

    struct estructure
    {
    unsigned control : 1; // one bit--> 0
    unsigned pedal : 2; // two bits--> 01
    } layout

    I need this 3 bits in a WORD variable like this: 001
    How can I do it?

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why did you highjack this thread instead of starting a new one? I know the topic is structs, but this is a different subject. At any rate, why don't you just use a union?
    Code:
    union foo
    {
        struct
        {
            unsigned int control:1;
            unsigned int pedal:2;
        } bar;
        WORD upG;
    };
    
    union foo x;
    WORD yo;
    
    x.bar.control = 0;
    x.bar.pedal = 1;
    yo = x.bar.upG;
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    but upG is empty... I dont understand

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read up on unions.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Question: I need to check for a phone number in the main file called Contacts it happens to be out on the 259 position on each row. My file that I use to check is just phone numbers starts on the 0 position. I need to load that file and check it with the Contact file. I'm clueless how to do this, Structs seem to be the most efficient way of doing this though. How do you load the files?
    For a bit I was doing alot of file IO but it was to slow as below..
    Code:
    while (phonefile.getline(phonenumber,12,'\n'))
    	 {
    	    ifstream filename21(filename, ios::in);
    		while (filename21.getline(stupidLine, lineSize)) 
    		{	
    			if ((strstr(stupidLine+258, phonenumber) !=0)) 
    				{	
    					fout3<<stupidLine<<endl;				
    				}
    		
    		}
              }
    Using a struct set me straight when I was searching for duplicates but I can't figure it out when I have a separate file called phone numbers that I have to use to eliminate some phone numbers in the big contacts file which their phone number again starts on the 259 position. If you all have some insights that would be great.

  7. #22
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    My previous post (dated 01-05-2004 06:44PM) explained how to do this. The post talking about the find_if function... remember? My example shows how to find something in a set (where the key is based on the 3 characters starting at offset 4) and match it with a string (3 characters long starting at offset 0). All you need to do is adapt that code to use different offsets ([edit]and lengths[/edit]) and put it in a loop. The outline of this loop should look roughly like:

    1. Read in a line from your phone number list into a string.
    2. Use the find_if function along with the function object (what I named Search in my post and what you will need to modify to use the correct offsets) to find the phone number in your set container.
    3. Take appropriate action based on whether the find_if function actually finds anything or not, i.e. erase that item from the set (my code example shows this as well).
    4. Go back to step 1.
    Last edited by hk_mp5kpdw; 01-10-2005 at 12:50 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #23
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Sorry for my ingnorance. I was about to write back to ask to explain futher but I looked at the code and it seems to make sense now.

    it = find_if(strSet.begin(),strSet.end(),Search("app")) ;
    Use this and just change "app" to my variable. And then change offsets. Then just have this all in a look until the end of the phone list..

    Thanks much..

  9. #24
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Trying to implement now and I ran into a problem, Error c2664
    Code:
    error C2664: 'class std::_Tree<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_
    traits<char>,class std::allocator<char> >,struct std::set<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct compare,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class st
    d::allocator<char> > > >::_Kfn,struct compare,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::iterator __thiscall std::set<class std::basic_string<char,struct std::char_traits<char>,c
    lass std::allocator<char> >,struct compare,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::erase(class std::_Tree<class std::basic_string<char,struct std::char_traits<char>,class std:
    :allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::set<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct compare,class std::allocator<cl
    ass std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::_Kfn,struct compare,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::iterator)' : cannot conv
    ert parameter 1 from 'class std::_Tree<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::set<class std::basic_s
    tring<char,struct std::char_traits<char>,class std::allocator<char> >,struct compare,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::_Kfn,struct compare,class std::allocator<class std
    ::basic_string<char,struct std::char_traits<char>,class 
    td::allocator<char> > > >::const_iterator' to 'class std::_Tree<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct st
    d::set<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct compare,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::_Kfn,struct compare,class
     std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >::iterator'
            No constructor could take the source type, or constructor overload resolution was ambiguous
    Error executing cl.exe.
    The error occurs when I try to remove string out of the it. When it gets to erase(it);

  10. #25
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    You're trying to remove an element with a const_iterator. You need an iterator.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  11. #26
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Excellent thanks much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM