Compare the Triplets

 1.  bits/stdc++.h      标准库头文件

 

 

2.  ofstream  fout()         文件输出流对象

     ofstream(const char *filename,openmode mode);

    ofstream fout("/temp/results.txt",ios::app);       将输出流对象中的内容追加到指定的文件当中

  指定的模式

  ios::app      添加输出    追加

  ios::ate       当已打开时寻找到EOF

  ios::binary  以二进制模式打开文件

  ios::in    为读取打开文件

  ios::out      为写入打开文件

  ios::trunc   覆盖存在的文件

  

3.  getenv("OUTPUT_PATH")      获取文件路径      括号中的参数为环境变量

    #include  <stdlib.h>  

  char * getenv(const char *name)

  返回环境变量name的值     依赖执行的情况   如果没有对应的环境变量name   则会返回null

  

4.  stoi()      string to int    字符串转换为int类型      

 

5.  string.erase()    清除 

  iterator erase(iterator pos);  删除指定位置的字符   返回执行下一个字符的迭代器。

  iterator erase(iterator start,iterator end);  删除迭代器区间包括的字符   返回一个指向被删除的最后一个字符的下一个位置的迭代器。

  basic_string & erase(size_type index=0,size_type num=npos);  从index索引处开始的num个字符   返回*this  指针。

  不带任何参数    则删除所有字符

  

string s("So, you like donuts, eh? Well, have all the donuts in the world!");
    cout << "The original string is '" << s << "'" << endl;
  
    s.erase( 50, 14 );                    //index      num
    cout << "Now the string is '" << s << "'" << endl;

    s.erase( 24 );                      //24之后的所有
    cout << "Now the string is '" << s << "'" << endl;

    s.erase();                          
    cout << "Now the string is '" << s << "'" << endl;  //删除所有字符

 

 

6.  string.rbegin()    逆向迭代器  右侧开始

   string.rend()

    reverse_iterator rbegin();  函数返回指向当前vector末尾的逆向迭代器      实际指向末尾的下一位置   

  

1 vector<int> v(10,20,30,40);
2 vector<int>::reverse_iterator it=v.rbegin();//逆向迭代器    rend   类似
3 cout<<*it<<endl;      返回40
4 cout<<*(it++)<<endl;    返回30    注意是逆向    

 

7.  not1()

   template <class Predicate> unary_negate<Predicate> not1 (const Predicate& pred);

  

1 template <class Predicate> unary_negate<Predicate> not1 (const Predicate& pred)
2 {
3   return unary_negate<Predicate>(pred);
4 }
// not1 example
#include <iostream>     // std::cout
#include <functional>   // std::not1
#include <algorithm>    // std::count_if

struct IsOdd {
  bool operator() (const int& x) const {return x%2==1;}
  typedef int argument_type;
};

int main () {
  int values[] = {1,2,3,4,5};
  int cx = std::count_if (values, values+5, std::not1(IsOdd()));
  std::cout << "There are " << cx << " elements with even values.\n";
  return 0;
}

 

8.  string.substr()

basic_string substr(size_type index,size_type num=npos);

返回本字符串的一个子串,从index开始   长度为num个字符    ,如果未指定   则返回index开始的剩余的所有字符

1     string s("What we have here is a failure to communicate");
2 
3     string sub = s.substr(21);
4 
5     cout << "The original string is " << s << endl;
6     cout << "The substring is " << sub << endl;

9.find()

1 size_type find(const basic_string &str,size_type index);
2 size_type find(const char *str,size_type index);
3 size_type find(const char *str,size_type index,size_type length);
4 size_type find(char ch,size_type index);

返回str在字符串中第一次出现的位置  从index开始查找,如果没找到则返回string::npos

返回str在字符串中第一次出现的位置  从index开始查找 长度为length    如果没找到就返回  string::npos

返回字符ch在字符串中第一次出现的位置  从index开始查找    如果没找到就返回string::npos

1 string str1("alpah beta gamma delta");
2 unsigned int loc=str1.find("omega",0);
3 if(loc!=string::npos)
4     cout<<"Found Omega at"<<loc<<endl;
5 else
6     cout<<"didn't find omega"<<endl;

 

10.find_if()

 
function template
<algorithm>

std::find_if

template <class InputIterator, class UnaryPredicate>
   InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);

等价于

1 template<class InputIterator, class UnaryPredicate>
2   InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
3 {
4   while (first!=last) {
5     if (pred(*first)) return first;
6     ++first;
7   }
8   return last;
9 }

举例:

  

 1 // find_if example
 2 #include <iostream>     // std::cout
 3 #include <algorithm>    // std::find_if
 4 #include <vector>       // std::vector
 5 
 6 bool IsOdd (int i) {
 7   return ((i%2)==1);
 8 }
 9 
10 int main () {
11   std::vector<int> myvector;
12 
13   myvector.push_back(10);
14   myvector.push_back(25);
15   myvector.push_back(40);
16   myvector.push_back(55);
17 
18   std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
19   std::cout << "The first odd value is " << *it << '\n';
20 
21   return 0;
22 }

 

 11.std::ptr_fun 

1 template <class Arg, class Result>
2 pointer_to_unary_function<Arg,Result> ptr_fun (Result (*f)(Arg));
3 
4 template <class Arg1, class Arg2, class Result>
5 pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun (Result (*f)(Arg1,Arg2));

 

 

 

 

 

完整代码:

 

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

// Complete the compareTriplets function below.
vector<int> compareTriplets(vector<int> a, vector<int> b) {
    vector<int> s{0,0};


        for(vector<int>::size_type index=0;index<3;index++)
        {
            if(a[index]>b[index])
                s[0]+=1;
            else if(a[index]<b[index])
                s[1]+=1;
            else continue;     //没有此代码也可
        }
      for(auto i:s)
       cout<<i<<endl;
    return s;

}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string a_temp_temp;
    getline(cin, a_temp_temp);

    vector<string> a_temp = split(rtrim(a_temp_temp));

    vector<int> a(3);

    for (int i = 0; i < 3; i++) {
        int a_item = stoi(a_temp[i]);

        a[i] = a_item;
    }

    string b_temp_temp;
    getline(cin, b_temp_temp);

    vector<string> b_temp = split(rtrim(b_temp_temp));

    vector<int> b(3);

    for (int i = 0; i < 3; i++) {
        int b_item = stoi(b_temp[i]);

        b[i] = b_item;
    }

    vector<int> result = compareTriplets(a, b);

    for (int i = 0; i < result.size(); i++) {
        fout << result[i];

        if (i != result.size() - 1) {
            fout << " ";
        }
    }

    fout << "\n";

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}

vector<string> split(const string &str) {
    vector<string> tokens;

    string::size_type start = 0;
    string::size_type end = 0;

    while ((end = str.find(" ", start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));

        start = end + 1;
    }

    tokens.push_back(str.substr(start));

    return tokens;
}

 

posted on 2020-12-28 14:28  学习记录园  阅读(117)  评论(0编辑  收藏  举报