Test if a vector contains a given element

Both the match() (returns the first appearance) and %in% (returns a Boolean) functions are designed for this. v <- c(‘a’,’b’,’c’,’e’) ‘b’ %in% v ## returns TRUE match(‘b’,v) ## returns the first location of ‘b’, in this case: 2

How do I erase an element from std::vector by index?

To delete a single element, you could do: std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); // Deletes the second element (vec[1]) vec.erase(std::next(vec.begin())); Or, to delete more than one element at once: // Deletes the second through third elements (vec[1], vec[2]) vec.erase(std::next(vec.begin(), 1), std::next(vec.begin(), 3));

What is the easiest way to initialize a std::vector with hardcoded elements?

If your compiler supports C++11, you can simply do: std::vector<int> v = {1, 2, 3, 4}; This is available in GCC as of version 4.4. Unfortunately, VC++ 2010 seems to be lagging behind in this respect. Alternatively, the Boost.Assign library uses non-macro magic to allow the following: #include <boost/assign/list_of.hpp> … std::vector<int> v = boost::assign::list_of(1)(2)(3)(4); Or: … Read more

Why is Java Vector (and Stack) class considered obsolete or deprecated?

Vector synchronizes on each individual operation. That’s almost never what you want to do. Generally you want to synchronize a whole sequence of operations. Synchronizing individual operations is both less safe (if you iterate over a Vector, for instance, you still need to take out a lock to avoid anyone else changing the collection at … Read more

How to find out if an item is present in a std::vector?

You can use std::find from <algorithm>: #include <algorithm> #include <vector> vector<int> vec; //can have other data types instead of int but must same datatype as item std::find(vec.begin(), vec.end(), item) != vec.end() This returns an iterator to the first element found. If not present, it returns an iterator to one-past-the-end. With your example: #include <algorithm> #include … Read more

Appending a vector to a vector [duplicate]

a.insert(a.end(), b.begin(), b.end()); or a.insert(std::end(a), std::begin(b), std::end(b)); The second variant is a more generically applicable solution, as b could also be an array. However, it requires C++11. If you want to work with user-defined types, use ADL: using std::begin, std::end; a.insert(end(a), begin(b), end(b));

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)