LoginSignup
0
0

More than 5 years have passed since last update.

[C++] Some useful method or tips in c++ (updating)

Last updated at Posted at 2015-07-17
  • Initialize a two dimension vector with some value
vector<vector<int>> dp(n, vector<int>(m, 2));
  • Get next iterator
//std::set<int>::iterator it;
std::next(it, 1)
  • Initialize any value for array/vector
//Fill a array with value
//int a[10]
fill(a, a + 10, 10);
//vector<int> b;
fill(b.begin(), b.end(), 10);
  • Get the max/min element
//Find the min/max value in array
*min_element(a, a + 7);
*max_element(a, a + 7);
  • Lower bound in set
//Find the value not less/greater than x in set(Ologn)
//std::set s;
s.lower_bound(x);
s.upper_bound(x);
  • Sum
//Sum values between i and j
accumulate(a, a+10, 0)
  • Lower bound in vector/array
//Find the value not less/greater than x in array/vector
upper_bound(a, a + 10, 5)
lower_bound(a, a + 10, 7)
  • Array copy
//Copy an array to another array
//int a[10], b[10]
copy(a, a + 10, b)
  • Substring
//sub string
s.substr(0, 10) // start position, length
  • Reverse string
//reverse string or vector
//string s;
reverse(s.begin(), s.end());
  • Sort in descending order
//sort in descending order of vector
//vector<int> a;
sort(a.begin(), a.end(), greater<int>());

//priority queue in increasing order.
priority_queue<int, vector<int>, greater<int> > q;
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0