Using CLists in c++ -Could not understand - c++

I searched for a sample program to work on with the CList.But i could not find it.I got some basic definitions about the CLists(template class that behaves similar to the doubly linked lists).. and is used to store the aggregate data..The 1st argument is stored internally by the list..I could not see what elements are stored in the list and how to retrieve it.
RsData Data; //object of class RsData
RsEvent* Event;//pointer to class RsEvent
CList<Event*,Event*> EventQueue;
Data.EventQueue.RemoveAll();
//removing the attributes and methods of the class RsEvent stored in the list.

So the doc is CList on MSDN
And there is a link there for the COLLECT Sample: Illustrates MFC Collection Classes
I'm not sure how this doc was not found, so perhaps your question needs to be clarrified.
The list offers front/back inserting and looking, aswell as methods for iterating.
// Define myList.
CList<CString,CString&> myList;
// Add an element to the front of the list.
myList.AddHead(CString("ABC"));
// Verify the element was added to the front of the list.
ASSERT(CString("ABC") == myList.GetHead());

Related

How to delete duplicate constructed objects in a list while preserving order and returning a List in dart?

I have a list of constructed objects called RecentCard which is basically an object with user id, pic, and name. I have created a list arranged in order of most recent interaction based on timestamp. However i need to get rid of the second occurence and onwards of any duplicated object. I am comparing just the ids since users may have the same name or photo, and i cannot remove duplicates from a simple list of uids because then i could lose the order of corresponding photos and names.
For example the list would look something like this :
List<RecentCard> recentCards= [RecentCard(uid:as721dn18j2,name:mike,photourl:https://sadadasd1d1),RecentCard(.....]
I have searched for solutions but all of them are dealing with like primitive types like simple lists of strings and the solutions didn't work for me. For example this post: How to delete duplicates in a dart List? list.distinct()? Read below
The first answer's link is no longer available, the next answer with sets is something i tried but it simply doesnt work i have no idea why maybe because its not a primitive type. The next answer with the queries package didn't work because i was using the list to build a listview.builder and so when i called list.length it said i couldnt call that on an iterable or something. The final solution wouldn't work because it said String is not a subtype of RecentCard.
I tried using two forloops with the second just comparing first value to the next couple but that doesnt work because if a duplicate is found, the object is removed and the length is messed up so some elements get skipped.
Any ideas? I feel like its very simple but im not sure how to do it.
You have to use Set to store seen cards and then filter away those ones that are already in the set.
final seenCards = Set<String>();
final uniqueCards = recentCards.where((card) => seenCards.add(card.uid)).toList();

exam C++ save name attribute of list

During my C++ exam I found I had some difficulties with what is probably supposed to be something relatively simple. I think I am overlooking something on my problem. For the sake of not getting in trouble for posting part of an exam online, I will make a very simple representation of the part I had troubles with.
What was expected was to make a class myClass in which i could add items item. Then i was supposed to be able to make lists of some items i wanted in that list. The amount of items able to be in the list should be infinite. Each list should have a certain (unique) name. I am also told not to make a new class for the lists and to choose the right container myself.
myClass::myClass(){
itemlist= {};
lists= {};
}
void myClass::addItem(Item &item){
itemlist.emplace_back(item);
}
void myClass::makeList(string listname){
vector <Item> list = {};
// list .name = listname
lists.emplace_back(list);
}
void myClass::addItemToList(string listName, Item &item){
for (int i=0; i<lists.size; i++){
if lists[i].name == listName {
lists.emplace_back(item);
return;
}
}
}
I don't know how to link a name to a list without creating a new class for the lists. I thought about storing another list containing the names of the lists and using that, but it seemed wrong to do so, as they aren't actually linked then. Tried to look up if there is a way to make a list variable listname from the string listname parameter, but apparently that's not possible.
As for the containers, I chose a vector as they can store an "infinite" amount of data in it. I use the itemlist to store all items ever added to myClass, and lists to store all the lists ever created.
In what way is it possible to store a name for each list and for it to be linked?
I think the simplest way to name lists is to use std::unordered_map http://en.cppreference.com/w/cpp/container/unordered_map
Then you'll be able to use your lists like this:
lists["first_list"].push_back(...);
lists["another_list"].size();
lists["create_new_list"] = {};
Maybe what you are looking for "link a name to a list" is a std::map
Something like
std::map<std::string, std::vector<Item>>

Why my output gives some garbage values, even though I input them

I was making my high school project and decided to use something like nested linked lists for some bonus marks. The aim of my project was to create a digital diary containing infinite no of pages and infinite no of lines per page. My program uses a linked list as a queue and each element in the queue has its own linked list as a queue. I am using arrays for headings and for each sub unit (line) of the nested queue and the gets and puts for input/output. My program displays the input data but not all correctly, the last elements of the array are sometimes smileys and arrows instead of what I put.I am using a structure for line, a class to use that queue and a derived class for the page which contains the heading, page no and the class containing lines. The derived class objects are now used as the bigger linked list in another class. Also, I wish to save the data to a binary file, please tell me whether should I store it as line by line or page by page. I am using C++
The only thing that comes to my mind is error in pointer dereferencing. Basically, your linked lists are pulling data from the wrong sector in memory. Try going through it again and see whether everything is referenced properly and data input is going where it must. Trying saving the data line by line to avoid again overflows or errors. If successful then try page by page.

Which data structure in C++ suits to implement webbrowser history?

In one of the interviews, I asked by one of the interviewers how to implement history of web browser but don't show duplicates and need to show in reverse order meaning from most recent till the 5th website visited.
I told we can use linked list. When user enters a website it will be checked against a list of nodes and if the site is already present in the list, we will remove it from the list and add it as head. If it is not in the list, it will be simply added as head of the list. But he told order of complexity is O(n*n) and he asked me are there any other data structures or combination of data structured we could use to make the order of complexity as O(n). I didn't get any clue at that time. Can any one please let me know if you have any idea.
You could do it in constant time if you are using your linked list plus a hash table with pointers to the list items.
um...
With a linked list, Add URL to the start of the list (O(1)), continue throught list, deleting if found (O(n))
As others have mentioned, Using a list and an unordered map would be your best bet. When visiting a new page, if the user has never visited it, add it to the end. if they have visited, recall the iterator using the hash map and remove it. The last step is to then add the url to the start of the list and give it a fresh new iterator in the map.
#include <list>
#include <unordered_map>
class browser
{
public:
void visit (const std::string& page)
{
auto location = locations.find(page);
if (location != locations.end())
{
pages.erase(location->second);
}
pages.push_front(page);
locations[page] = pages.begin();
}
private:
using Pages = std::list<std::string>;
using Locations = std::unordered_map<std::string, Pages::iterator>;
Pages pages;
Locations locations;
};

How do i create and populate a linked list using classes in c++?

I am trying to create a program which uses classes to create a linked list data structure. Is it necessary to create one linked list class which contains the list in it's entirety: nodes with info and the list? or do i create a class for the list and a class for the nodes in the list?
Thanks,
Don't create your own list - use std::list