Solutions for a junior Cpp problem about STL

This problem is published on HackerRand.

Problem description : 

You are provided with a vector of  integers. Then, you are given  queries. For the first query, you are provided with  integer, which denotes a position in the vector. The value at this position in the vector needs to be erased. The next query consists of  integers denoting a range of the positions in the vector. The elements which fall under that range should be removed. The second query is performed on the updated vector which we get after performing the first query.

The following are some useful vector functions:

 

  • erase(int position):
    Removes the element present at position.  
    Ex: v.erase(v.begin()+4); (erases the fifth element of the vector v)
  • erase(int start,int end):
  • Removes the elements in the range from start to end inclusive of the start and exclusive of the end.
    Ex:v.erase(v.begin()+2,v.begin()+5);(erases all the elements from the third element to the fifth element.)

 

Input Format

The first line of the input contains an integer .The next line contains  space separated integers(1-based index).The third line contains a single integer ,denoting the position of an element that should be removed from the vector.The fourth line contains two integers  and  denoting the range that should be erased from the vector inclusive of a and exclusive of b.
 

Constraints
 
 

Output Format

Print the size of the vector in the first line and the elements of the vector after the two erase operations in the second line separated by space.

Sample Input

6
1 4 6 2 8 9
2
2 4

Sample Output

3
1 8 9

Explanation

The first query is to erase the 2nd element in the vector, which is 4. Then, modifed vector is {1 6 2 8 9}, we want to remove the range of 2~4, which means the 2nd and 3rd elements should be removed. Then 6 and 2 in the modified vector are removed and we finally get {1 8 9}

 

Solutions : 

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;

// version 1
// int main() {
//     /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
//     int n;
//     cin >> n;
//     vector<int> v(n,0);
//     for(int i = 0; i < n; i++){
//         cin >> v[i];
//     }
//     int p;
//     int rs, re;
//     cin >> p >> rs >> re;
//     v.erase(v.begin() + p - 1);
//     v.erase(v.begin() + rs - 1, v.begin() + re - 1);
//     cout << v.size() << endl;
//     for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
//         cout << *it << ' ';
//     return 0;
// }


// version 2 - copy function, istream_intertor, ostream_iterator
// int main() {
//     /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
//     int n;
//     cin >> n;
//     vector<int> v;
//     copy_n(istream_iterator<int>(cin), n, back_inserter(v));
//     int p;
//     int rs, re;
//     cin >> p >> rs >> re;
//     v.erase(v.begin() + p - 1);
//     v.erase(v.begin() + rs - 1, v.begin() + re - 1);
//     cout << v.size() << endl;
//     copy(begin(v), end(v), ostream_iterator<int>(cout, " "));
//     return 0;
// }


// version 3 - for_each with Lambda function
// int main() {
//     /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
//     int n;
//     cin >> n;
//     vector<int> v;
//     copy_n(istream_iterator<int>(cin), n, back_inserter(v));
//     int p;
//     int rs, re;
//     cin >> p >> rs >> re;
//     v.erase(v.begin() + p - 1);
//     v.erase(v.begin() + rs - 1, v.begin() + re - 1);
//     cout << v.size() << endl;
//     for_each(v.begin(), v.end(), [](int i){cout << i << " ";});
//     return 0;
// }

// version 4 - for_each with function
// void func(int i){
//     cout << i << " ";
// }

// int main() {
//     /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
//     int n;
//     cin >> n;
//     vector<int> v;
//     copy_n(istream_iterator<int>(cin), n, back_inserter(v));
//     int p;
//     int rs, re;
//     cin >> p >> rs >> re;
//     v.erase(v.begin() + p - 1);
//     v.erase(v.begin() + rs - 1, v.begin() + re - 1);
//     cout << v.size() << endl;
//     for_each(v.begin(), v.end(), func);
//     return 0;
// }

// version 5 - for_each with struct
// struct myfunc{
//     void operator () (int i) {cout << i << " ";} 
// } func;

// int main() {
//     /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
//     int n;
//     cin >> n;
//     vector<int> v;
//     copy_n(istream_iterator<int>(cin), n, back_inserter(v));
//     int p;
//     int rs, re;
//     cin >> p >> rs >> re;
//     v.erase(v.begin() + p - 1);
//     v.erase(v.begin() + rs - 1, v.begin() + re - 1);
//     cout << v.size() << endl;
//     for_each(v.begin(), v.end(), func);
//     return 0;
// }

// version 6 - for_each with class
// class myfunc{
// public:
//     void operator () (int i) {cout << i << " ";} 
// } func;

// int main() {
//     /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
//     int n;
//     cin >> n;
//     vector<int> v;
//     copy_n(istream_iterator<int>(cin), n, back_inserter(v));
//     int p;
//     int rs, re;
//     cin >> p >> rs >> re;
//     v.erase(v.begin() + p - 1);
//     v.erase(v.begin() + rs - 1, v.begin() + re - 1);
//     cout << v.size() << endl;
//     for_each(v.begin(), v.end(), func);
//     return 0;
// }

// version 7 - for
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n;
    cin >> n;
    vector<int> v;
    copy_n(istream_iterator<int>(cin), n, back_inserter(v));
    int p;
    int rs, re;
    cin >> p >> rs >> re;
    v.erase(v.begin() + p - 1);
    v.erase(v.begin() + rs - 1, v.begin() + re - 1);
    cout << v.size() << endl;
    for(int i : v) { cout << i << ' ';}
    return 0;
}

 

C++ STL是C++标准模板库(Standard Template Library)的简称。它是C++的一个重要组成部分,提供了一系列的通用模板类和函数,用于处理常见的数据结构和算法问题。 学习C++ STL有以下几个方面的好处: 1. 提高开发效率:STL提供了大量现成的数据结构和算法,比如向量(vector)、链表(list)、队列(queue)、堆栈(stack)等,以及排序、查找、计数、遍历等算法。使用STL可以避免重复造轮子的过程,通过简单的调用就可以快速编写高效的代码,提高开发效率。 2. 提高代码质量:STL是由专业的C++程序员设计和实现的,其设计遵循了面向对象的思想,并使用了模板元编程等技术。使用STL可以提高代码的模块化程度,减少重复代码,使代码更加清晰、简洁和可维护。 3. 为学习其他编程语言打下基础:STL采用了一种通用、抽象的设计,其思想和理念对于学习其他编程语言也是有借鉴意义的。通过学习STL,可以更好地理解数据结构和算法的设计与实现,为学习其他编程语言打下坚实的基础。 要学习C++ STL,可以从以下几个方面入手: 1. 理解STL的组成部分:了解STL的组成部分,包括容器(container)、迭代器(iterator)、算法(algorithm)、函数对象(function object)、适配器(adapter)等。理解它们之间的关系和作用,掌握各个组成部分的用法和特点。 2. 学习STL的常用容器和算法:熟悉STL提供的常用容器和算法,如向量(vector)、链表(list)、队列(queue)、堆栈(stack)等,以及排序、查找、计数、遍历等算法。了解其基本的操作和用法,掌握它们的时间复杂度和使用场景。 3. 理解STL内部实现原理:了解STL内部的实现原理,包括对容器和算法的底层实现,例如迭代器的实现、算法的实现方式和优化等。理解这些原理有助于更好地理解和使用STL,以及优化代码性能。 总之,学习C++ STL对于提高C++编程能力和开发效率非常重要。通过学习STL,可以更好地掌握C++的数据结构和算法,提高代码质量和可维护性,为进一步学习和应用其他编程语言打下坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值