Question

In: Computer Science

C++ mainDisplay.cpp Write the function displayAt that receives the two STL lists; vList and pList. The...

C++ mainDisplay.cpp

Write the function displayAt that receives the two STL lists; vList and pList. The function should display the elements in vList that are in positions specified by pList. For example, if pList has the elements (2, 5, 6, 8) then the elements in positions 2, 5, 6, and 8 in vList are displayed. Use only the public STL container operations.

Solutions

Expert Solution

Solution:

I have written the function in C++ 11

Note:

Since It is mentioned as positions so the numbering starts from 1 not from 0. .

If you require any changes in the function please comment and I will do the needful.

Code of the function:

void displayAt(list <int> vList,list <int> pList)
{
        list <int> :: iterator it=pList.begin(); 

        for (int val : vList)
        {
                it=next(pList.begin(), val-1);
                cout<<"Element at position "<<val<<": "<<*it<<endl;

        }
        
}

Complete implementation with code:

#include <iostream> 
#include <list> 

using namespace std;

void displayAt(list <int> vList,list <int> pList)
{
        list <int> :: iterator it=pList.begin(); 

        for (int val : vList)
        {
                it=next(pList.begin(), val-1);
                cout<<"Element at position "<<val<<": "<<*it<<endl;

        }
        
}

int main()
{
        list <int> vList({2,5,6,8});
        list <int> pList({1,2,3,4,5,6,7,8,9,10});
        displayAt(vList,pList);
        return 0;
}

Screenshot of output:


Related Solutions

write a member function in C++ , that takes two lists and return list that contain...
write a member function in C++ , that takes two lists and return list that contain the merge of the two lists in the returned list: first insert the first list and then the second list  
C++ and leave comments explaining. Thank you You are given two STL lists X and P...
C++ and leave comments explaining. Thank you You are given two STL lists X and P where n P is already in sorted order. Write a valid C++ function printPositions(X,P) that prints the elements in X specified by P. For example, if P = 0, 3, 7, 8, the elements in positions 0 (head of the list), 3, 7, and 8 in X are printed. You may use only the public STL container operations. Also specify the running time of...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following order: An array of integer values (array1). An integer representing the size of array1 (size1). An array of integer values (array2). An integer representing the size of array2 (size). The function creates a dynamic array of integers of size size1+size2 to store all the values in array1, followed by all the values in array2. The function returns the pointer used to create the dynamic...
Just need the desktop test for this two function a) Write a function that receives the...
Just need the desktop test for this two function a) Write a function that receives the number of terms (n) and returns a container with the first n TunaPoke numbers. Present a "print screen" of the results and their respective desktop test when the function is invoked with the 16. (b) Write a function that receives a limit number and returns a container with the first numbers TunaPoke that do not exceed the specified limit. Present a "print screen" of...
Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
A tree can be considered as a structured graph. Write a C++ function that receives a...
A tree can be considered as a structured graph. Write a C++ function that receives a BST object and returns its corresponding graph representation, implemented via an adjacency list. What will be the impact on the search complexity?
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
C++ PROJECT Create a sudoku game that involves the following concepts: STL library. (Maps, Sets, Lists,...
C++ PROJECT Create a sudoku game that involves the following concepts: STL library. (Maps, Sets, Lists, Stacks and Queues), with Iterators and Algorithms Show especially Especially Algorithm/Iterators/Containers in the STL Minimum of 900 lines Include Full documentation
This is the question Write a function add(vals1, vals2) that takes as inputs two lists of...
This is the question Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers, vals1 and vals2, and that uses recursion to construct and return a new list in which each element is the sum of the corresponding elements of vals1 and vals2. You may assume that the two lists have the same length. For example: >>> add([1, 2, 3], [3, 5, 8]) result: [4, 7, 11] Note that: The first element of the...
Write a function sublist that takes two lists as arguments, and returns true if the first...
Write a function sublist that takes two lists as arguments, and returns true if the first list appears as a contiguous sublist somewhere within the second list, and false otherwise. > (sublist ’(c d e) ’(a b c d e f g)) #t > (sublist ’(a c e) ’(a b c d e f g)) #f Write a function lgrep that returns the “lines” within a list that contain a given sublist. Use the sublist function implemented in previous exercise...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT