For C++ Thanks in Advance! 1) Write a while loop that reads integers from standa
ID: 3864701 • Letter: F
Question
For C++
Thanks in Advance!
1) Write a while loop that reads integers from standard input and prints them on standard output separated by spaces until the end of file on standard input is encountered.
2) Write a program that reads strings from stdin, inserts them in a set container, and prints them sorted on stdout.
3) Express the cost of finding an element in a balanced binary search tree, as a function of the number N of elements in the tree.
4)Write a range-based for loop to print on stdout all the elements of a list declared as list a;
Explanation / Answer
1)
#include <iostream>
using namespace std;
int main()
{
int val=0;
// continue until eof
while(!cin.eof())
{
// take input
cin>>val;
// display value
cout<<val<<" ";
}
return 0;
}
2)
#include <iostream>
#include <set>
using namespace std;
int main ()
{
// declare set of string
set<string> s;
string str;
// set of string does srting of its own
while(!cin.eof())
{
cin>>str;
s.insert(str);
}
cout << "Set contains:";
while (!s.empty()) {
cout << ' ' << *s.begin();
s.erase(s.begin());
}
return 0;
}
4)
#include <iostream>
#include <list>
using namespace std;
int main ()
{
list<int> s(4,100);
for (list<int>::iterator it = s.begin(); it != s.end(); it++)
cout << *it << ' ';
cout << ' ';
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.