More deque questions

This is a discussion on More deque questions within the C++ Programming forums, part of the General Programming Boards category; This is throwing errors; Code: void Data::addJob(string TempName, int TempDate, string TempDesc) { for (int i = 0; i < ...

  1. #1
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100

    More deque questions

    This is throwing errors;

    Code:
    void Data::addJob(string TempName, int TempDate, string TempDesc)
    {
        for (int i = 0; i < Name.size(); i++){
                if(TempDate > Date[Date.begin() + i]){
                    Name.insert(Name.begin()+i, TempName);
                    Date.insert(Date.begin()+i, TempDate);
                    Description.insert(Description.begin()+i, TempDesc);
                    break;
                }
        }
    
    }
    Code:
    |In member function `void Data::addJob(std::string, int, std::string)':|
    |21|warning: comparison between signed and unsigned integer expressions|
    |22|error: no match for 'operator[]' in '((Data*)this)->Data::Date[std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator+(ptrdiff_t) const [with _Tp = int, _Ref = int&, _Ptr = int*](i)]'|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_deque.h|884|note: candidates are: typename _Alloc::reference std::deque<_Tp, _Alloc>::operator[](size_t) [with _Tp = int, _Alloc = std::allocator<int>]|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_deque.h|897|note:                 typename _Alloc::const_reference std::deque<_Tp, _Alloc>::operator[](size_t) const [with _Tp = int, _Alloc = std::allocator<int>]|
    ||=== Build finished: 1 errors, 1 warnings ===|
    Last edited by Terran; 06-27-2008 at 07:23 PM.
    Sorry, but i'm a Code::Blocks man now.

  2. #2
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    So i changed the references to the iterators to a integer that keeps count of all "jobs". I can't seem to get it to sort correctly though. Although i have had a few beers lol.
    Sorry, but i'm a Code::Blocks man now.

  3. #3
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Why does it "seem" to sort correctly this way even though my brain is telling me that on the last part of the add function s it should be if(TempDate > Date[(DCounter-1) + i])
    Code:
    class Data
    {
        public:
        Data():DCounter(0) {};
        ~Data() {Name.clear(); Date.clear(); Description.clear();};
        void addJob(string TempName, int TempDate, string TempDesc);
        void showAll();
    
    
        private:
            deque <string> Name;
            deque <int> Date;
            deque <string> Description;
            int DCounter;
    };
    void Data::addJob(string TempName, int TempDate, string TempDesc)
    {
        if(DCounter == 0){
            Name.push_back(TempName);
            Date.push_back(TempDate);
            Description.push_back(TempDesc);
            DCounter++;
        }
        else if(DCounter == 1){
            if (Date[0] < TempDate){
                Name.push_front(TempName);
                Date.push_front(TempDate);
                Description.push_front(TempDesc);
                DCounter++;
            }
            else {
                Name.push_back(TempName);
                Date.push_back(TempDate);
                Description.push_back(TempDesc);
                DCounter++;
            }
        }
        else {
            for (int i = 0; i < DCounter; i++){
                if(TempDate > Date[(DCounter) + i]){
                    Name.insert(Name.begin()+i, TempName);
                    Date.insert(Date.begin()+i, TempDate);
                    Description.insert(Description.begin()+i, TempDesc);
                    DCounter++;
                    break;
                }
            }
        }
    
    }
    edit: it half works, if i call it with;
    Code:
        JobList->addJob("Dentist", 612, "");
         JobList->addJob("M0", 623, "");
        JobList->addJob("M1", 622, "");
        JobList->addJob("M2", 624, "");
        JobList->addJob("M3", 610, "");
    it outputs

    Code:
            Task            Date
    0:      M3              610
    1:      M2              624
    2:      M0              623
    3:      M1              622
    4:      Dentist         612
    Last edited by Terran; 06-27-2008 at 08:18 PM.
    Sorry, but i'm a Code::Blocks man now.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Is DCounter a count of how many items are in the deque? Why not deque::size() ? If it is, then this:
    Code:
    Date[(DCounter) + i]
    Is out of range.

    Some suggestions:
    Do you have "Jobs" that consist of a date, name, and description? Why not then keep one deque that contains a structure that holds date/name/description triples?
    Also, is a deque the right container to use? Why not use a sorted container, like set, map, multimap, etc, if you want things to always be sorted? Or, if you want a deque, why not use std::sort() ?
    Data also seems a poor name for a class. There's also not much point in your destructor - the deques will correctly destruct here.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Keeping related data together would greatly simplify what you are doing.

    Another thing is using std::algorithms to do the job for you. Inserting something into a deque while keeping it sorted might look like this:
    Code:
    std::deque<int>::iterator pos = std::upper_bound(Date.begin(), Date.end(), tempDate, std::greater<int>());
    Date.insert(pos, tempDate);
    You can use the distance between pos and Date.begin() to insert things into parallel containers at the same distance.

    That is of course if you choose not use some of the sorted containers (priority_queue might also be worth considering).
    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).

  6. #6
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Quote Originally Posted by Cactus_Hugger View Post
    Is DCounter a count of how many items are in the deque? Why not deque::size() ? If it is, then this:
    Code:
    Date[(DCounter) + i]
    Is out of range.

    Some suggestions:
    Do you have "Jobs" that consist of a date, name, and description? Why not then keep one deque that contains a structure that holds date/name/description triples?
    Also, is a deque the right container to use? Why not use a sorted container, like set, map, multimap, etc, if you want things to always be sorted? Or, if you want a deque, why not use std::sort() ?
    Data also seems a poor name for a class. There's also not much point in your destructor - the deques will correctly destruct here.
    i wasn't sure about the destructor. But i was using deques because of their random insertion ability. but you may be right about using a struct.
    Sorry, but i'm a Code::Blocks man now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deque
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 02-29-2008, 01:35 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  3. Confusion with a Deque program
    By Bluefish in forum C++ Programming
    Replies: 0
    Last Post: 05-20-2006, 04:13 PM
  4. costum grow() not working
    By Opel_Corsa in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2006, 10:11 PM
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21