In C++ Write one line of code for each line below. What is the template type in
ID: 3860910 • Letter: I
Question
In C++ Write one line of code for each line below. What is the template type in each case?
- Create a vector to store student IDs
- Create a vector to store student names
- Create a vector to store Point2D points (x, y)
- Create a set to store unique (no duplicates) student names
- Create a set to store unique (no duplicates) student IDs
- Create a map that maps student IDs to their score
- Create a map that maps student IDs to their name
- Create a map that maps student names to the name of their lab partner
Explanation / Answer
typedef struct
{
int ID;
string name;
string surname;
int points;
}
Student;
int main()
{
ifstream theFile("test.txt");
std::vector<Student*> students;
Student* s = new Student();
while(theFile >> s->ID >> s->name >> s->surname >> s->points)
{
studenti.push_back(s); // here I would like to add this struct s from a file
}
// here I want to print each struct's values on the screen, but the output is always ONLY last struct N times, and not all of them, each only once
std::vector<Student*>::const_iterator it;
for(it = students.begin(); it != students.end(); it+=1)
{
std::cout << (*it)->ID <<" " << (*it)->name << " " << (*it)->surname <<" " << (*it)->points <<endl;
}
==============================================================================================
public Scanner _scan = new Scanner(System.in);
Add_Student _added = new Add_Student();
public Student_Records(){
int _mychoice = choices();
try{
switch(_mychoice){
case 1:
_added.add();break;
default:
System.out.println("Choose only between 1 to 5");
}
}catch(NumberFormatException err){
System.out.println("Choose only between 1 to 5!!");
}
}
private int choices(){
System.out.print("---------------------------------- " +
"| 1: 'ADD' Student | " +
"| 2: 'EDIT' Student | " +
"| 3: 'DELETE' Student | " +
"| 4: 'UPDATE' Student | " +
"| 5: 'QUIT' | " +
"---------------------------------- " +
"Choose: ");
int _mychoice = _scan.nextInt();
return _mychoice;
}
public static void main(String[] args) throws Exception{
new Student_Records();
}
}
====================================================================================================
#include <iostream>
struct Point{ int X; int Y;};
typedef Point* PointsArr;
static const int size=100;
void main()
{
PointsArr test=new Point[size];
memset(test,0,size*sizeof(Point));
for(int i=0;i<size;i++)
{
test[i].X=i*2;
test[i].Y=(i*2)+1;
}
for(int i=0;i<size;i++)
{
std::cout<<test[i].X<<" "<<test[i].Y<<std::endl;
}
}=====================================================================
template<typename T>
struct vector_set
{
using vec_type = std::vector<T>;
using const_iterator = typename vec_type::const_iterator;
using iterator = typename vec_type::iterator;
vector_set(size_t max_size)
: _max_size { max_size }
{
_v.reserve(_max_size);
}
/// @returns: pair of iterator, bool
/// If the value has been inserted, the bool will be true
/// the iterator will point to the value, or end if it wasn't
/// inserted due to space exhaustion
auto insert(const T& elem)
-> std::pair<iterator, bool>
{
if (_v.size() < _max_size) {
auto it = std::lower_bound(_v.begin(), _v.end(), elem);
if (_v.end() == it || *it != elem) {
return make_pair(_v.insert(it, elem), true);
}
return make_pair(it, false);
}
else {
return make_pair(_v.end(), false);
}
}
auto find(const T& elem) const
-> const_iterator
{
auto vend = _v.end();
auto it = std::lower_bound(_v.begin(), vend, elem);
if (it != vend && *it != elem)
it = vend;
return it;
}
bool contains(const T& elem) const {
return find(elem) != _v.end();
}
const_iterator begin() const {
return _v.begin();
}
const_iterator end() const {
return _v.end();
}
private:
vec_type _v;
size_t _max_size;
};
using namespace std;
BOOST_AUTO_TEST_CASE(play_unique_vector)
{
vector_set<int> v(100);
for (size_t i = 0 ; i < 1000000 ; ++i) {
v.insert(int(random() % 200));
}
cout << "unique integers:" << endl;
copy(begin(v), end(v), ostream_iterator<int>(cout, ","));
cout << endl;
cout << "contains 100: " << v.contains(100) << endl;
cout << "contains 101: " << v.contains(101) << endl;
cout << "contains 102: " << v.contains(102) << endl;
cout << "contains 103: " << v.contains(103) << endl;
}
=============================================================================
function ArrNoDupe(a) {
var temp = {};
for (var i = 0; i < a.length; i++)
temp[a[i]] = true;
var r = [];
for (var k in temp)
r.push(k);
return r;
}
$(document).ready(function() {
var arr = [10, 7, 8, 3, 4, 3, 7, 6];
var noDupes = ArrNoDupe(arr);
$("#before").html("Before: " + arr.join(", "));
$("#after").html("After: " + noDupes.join(", "));
});
=======================================================================================def getLetterGrade(score):
score = round(score)
if score >= 90: return "A"
if 90 > score >= 80: return "B"
if 80 > score >= 70: return "C"
if 70 > score >= 60: return "D"
if 60 > score: return "F"
=====================================================================================
import java.util.*;
public class EmployeeSalaryStoring {
public static void main(String[] args) {
//Below Line will create HashMap with initial size 10 and 0.5 load factor
Map<String, Integer>empSal = new HashMap<String, Integer>(10, 0.5f);
//Adding employee name and salary to map
empSal.put("Ramesh", 10000);
empSal.put("Suresh", 20000);
empSal.put("Mahesh", 30000);
empSal.put("Naresh", 1000);
empSal.put("Nainesh", 15000);
empSal.put("Rakesh", 10000); // Duplicate Value also allowed but Keys should not be duplicate
empSal.put("Nilesh", null); //Value can be null as well
System.out.println("Original Map: "+ empSal);// Printing full Map
//Adding new employee the Map to see ordering of object changes
empSal.put("Rohit", 23000);
//Removing one key-value pair
empSal.remove("Nilesh");
System.out.println("Updated Map: "+empSal);// Printing full Map
//Printing all Keys
System.out.println(empSal.keySet());
//Printing all Values
System.out.println(empSal.values());
}
}
========================================================
map<string,int>::iterator itr=m.find(val); //Gives the iterator to the element val if it is found otherwise returns m.end() .
Ex: map<string,int>::iterator itr=m.find("Maps"); //If Maps is not present as the key value then itr==m.end().