本文整理汇总了C++中std::begin方法的典型用法代码示例。如果您正苦于以下问题:C++ std::begin方法的具体用法?C++ std::begin怎么用?C++ std::begin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std
的用法示例。
在下文中一共展示了std::begin方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
vector<int> ivec;
#ifdef LIST_INIT
vector<int> v = {0,1,2,3,4,5,6,7,8,9};
#else
int temp[] = {0,1,2,3,4,5,6,7,8,9};
vector<int> v(begin(temp), end(temp));
#endif
// range variable must be a reference so we can write to the elements
for (auto &r : v) // for each element in v
r *= 2; // double the value of each element in v
// print every element in v
for (int r : v)
cout << r << " "; // print the elements in v
cout << endl;
return 0;
}
示例2: resolve_overloads
overload_set resolve_overloads(Set const& overloads, Args...) {
overload_set candidates;
using std::begin;
using std::end;
std::copy_if(begin(overloads), end(overloads), std::back_inserter(candidates), [](auto potential_candidate) -> bool {
using boost::fusion::all;
using boost::fusion::transform;
boost::phoenix::arg_names::arg1_type identity;
return potential_candidate.size() == sizeof...(Args) && all(
transform(
detail::fusion_tie<sizeof...(Args)>::call(potential_candidate),
std::tie(typeid(Args)...),
std::equal_to<std::type_index>()
), identity);
});
return candidates;
}
示例3: findAndInsert
void findAndInsert(C& container, // in container, find
const V& targetVal, // first occurrence
const V& insertVal) // of targetVal, then
{ // insert insertVal
// using std::cbegin; // there
// using std::cend;
// auto it = std::find(cbegin(container), // non-member cbegin
// cend(container), // non-member cend
// targetVal);
// Support of `std::cbegin()`in C++14
// url: http://stackoverflow.com/a/31038315
using std::begin; // there
using std::end;
auto it = std::find(begin(container), // non-member cbegin
end(container), // non-member cend
targetVal);
container.insert(it, insertVal);
}
示例4: check
bool check(T&& t) {
using std::begin;
std::regex r{ s_ };
std::match_results<decltype(begin(t))> results;
if (std::regex_match(std::forward<T>(t), results, r)) {
// The first sub_match is the whole string; the next
// sub_match is the first parenthesized expression.
if (results.size() != len + 1) {
return false;
}
else{
for (std::size_t i = 1; i <= ar_.size();++i) {
ar_[i - 1] = results[i].str();
}
return match_check(get_tuple(), t_);
}
}
else {
return false;
}
}
示例5: read
bool Json_Plugin::poll_request(net::req::Request& req) noexcept
{
io_->step();
if(!bufs_.size()) return false;
Json::Value val;
Json::Reader read(Json::Features::strictMode());
using std::begin; using std::end;
// While the queued buffer is failing to compile.
while(!read.parse(std::string(begin(bufs_.front()), end(bufs_.front())),
val))
{
// Ignore it and continue on.
bufs_.pop();
if(!bufs_.size()) return false;
}
bufs_.pop();
req = FORMATTER_TYPE(net::req::Request)::parse(val);
return true;
}
示例6: SaveUnweightedDistances
void SaveUnweightedDistances(const StudentNetwork& network,
const string& filename,
FilterFunc filter) {
ofstream bfs_file{filename};
for (auto vertex_d : network.GetVertexDescriptors()) {
auto student_id = network[vertex_d];
// Determine whether or not we should process this student.
if (!filter(student_id)) { continue; }
bfs_file << network[vertex_d] << "\t";
// unweighted distance stats
auto unweighted_distances = network.FindUnweightedDistances(vertex_d);
// get the second member of the pair
auto distance_values = vector<string>{};
transform(begin(unweighted_distances), end(unweighted_distances),
back_inserter(distance_values),
[](pair<Student::Id, double> elt)
{ return to_string(elt.second); });
bfs_file << join(distance_values, "\t") << endl;
}
}
示例7: sort_
namespace detail {
using std::begin;
using std::end;
template<typename Container, typename Compare>
inline void sort_(Container& container, Compare&& comp, ...) {
std::sort(begin(container), end(container), std::forward<Compare>(comp));
}
template<typename Container, typename Compare>
inline auto sort_(Container& container, Compare&& comp, int) ->
decltype(container.sort(std::forward<Compare>(comp))) {
return container.sort(std::forward<Compare>(comp));
}
template<typename Container, typename Compare = std::less<
typename std::decay<
decltype(*begin(std::declval<Container&>()))
>::type
>>
inline void sort(Container& container, Compare&& comp = {}) {
sort_(container, std::forward<Compare>(comp), 0);
}
} // namespace detail
示例8: main
int main()
{
int ia[] = {0,1,2,3,4,5,6,7,8,9};
int *p = ia; // p points to the first element in ia
++p; // p points to ia[1]
int *e = &ia[10]; // pointer just past the last element in ia
for (int *b = ia; b != e; ++b)
cout << *b << " "; // print the elements in ia
cout << endl;
const size_t sz = 10;
int arr[sz]; // array of 10 ints
for (auto &n : arr) // for each element in arr
cin >> n; // read values from the standard input
for (auto i : arr)
cout << i << " ";
cout << endl;
// pbeg points to the first and
// pend points just past the last element in arr
int *pbeg = begin(arr), *pend = end(arr);
// find the first negative element,
// stopping if we've seen all the elements
while (pbeg != pend && *pbeg >= 0)
++pbeg;
if (pbeg == pend)
cout << "no negative elements in arr" << endl;
else
cout << "first negative number was " << *pbeg << endl;
return 0;
}
示例9: query
Container query(TQuery&& q, multiple_results_tag)
{
using std::to_string; using std::begin; using std::end;
formatter fmt { };
const std::string qry = to_string(fix::forward<TQuery>(q), fmt);
if(0 != _context->query(qry.c_str())) {
throw std::runtime_error("Could not query database");
}
mysql::basic_result res(*_context);
if(res) {
Container ret;
ret.reserve(res.rows());
std::transform(
begin(res),
end(res),
std::back_inserter(ret),
[](const basic_row& r) {
return impl::make_record<TRecord>::make(r);
}
);
return ret;
}
throw std::runtime_error("No valid context available");
}
示例10: compare
bool compare( Range1&& range1, Range2&& range2, Pred&& pred, RangeCapacityCompare&& capacitypred ) {
using std::begin;
using std::end;
return compare( begin( range1 ), end( range1 ), begin( range2 ), end( range2 ), std::forward<Pred>( pred ), std::forward<RangeCapacityCompare>( capacitypred ) );
}
示例11: end
proxy & operator =(Type && val)
{
using std::begin; using std::end;
str->append(begin(val), end(val));
return *this;
}
示例12: main
int main(int, char**) {
// This example will cover the read-only BSON interface.
// Lets first build up a non-trivial BSON document using the builder interface.
using builder::basic::kvp;
using builder::basic::sub_array;
auto doc = builder::basic::document{};
doc.append(kvp("team", "platforms"), kvp("id", types::b_oid{oid()}),
kvp("members", [](sub_array sa) {
sa.append("tyler", "jason", "drew", "sam", "ernie", "john", "mark", "crystal");
}));
// document::value is an owning bson document conceptually similar to string.
document::value value{doc.extract()};
// document::view is a non-owning bson document conceptually similar to string_view.
document::view view{value.view()};
// Note: array::view and array::value are the corresponding classes for arrays.
// we can print a view using to_json
std::cout << to_json(view) << std::endl;
// note that all of the interesting methods for reading BSON are defined on the view type.
// iterate over the elements in a bson document
for (document::element ele : view) {
// element is non owning view of a key-value pair within a document.
// we can use the key() method to get a string_view of the key.
stdx::string_view field_key{ele.key()};
std::cout << "Got key, key = " << field_key << std::endl;
// we can use type() to get the type of the value.
switch (ele.type()) {
case type::k_utf8:
std::cout << "Got String!" << std::endl;
break;
case type::k_oid:
std::cout << "Got ObjectId!" << std::endl;
break;
case type::k_array: {
std::cout << "Got Array!" << std::endl;
// if we have a subarray, we can access it by getting a view of it.
array::view subarr{ele.get_array().value};
for (array::element ele : subarr) {
std::cout << "array element: " << to_json(ele.get_value()) << std::endl;
}
break;
}
default:
std::cout << "We messed up!" << std::endl;
}
// usually we don't need to actually use a switch statement, because we can also
// get a variant 'value' that can hold any BSON type.
types::value ele_val{ele.get_value()};
// if we need to print an arbitrary value, we can use to_json, which provides
// a suitable overload.
std::cout << "the value is " << to_json(ele_val) << std::endl;
;
}
// If we want to search for an element we can use operator[]
// (we also provide a find() method that returns an iterator)
// Note, this does a linear search so it is O(n) in the length of the BSON document.
document::element ele{view["team"]};
if (ele) {
// this block will execute if ele was actually found
std::cout << "as expected, we have a team of " << to_json(ele.get_value()) << std::endl;
}
// Because view implements begin(), end(), we can also use standard STL algorithms.
// i.e. if we want to find the number of keys in a document we can use std::distance
using std::begin;
using std::end;
auto num_keys = std::distance(begin(view), end(view));
std::cout << "document has " << num_keys << " keys." << std::endl;
// i.e. if we want a vector of all the keys in a document, we can use std::transform
std::vector<std::string> doc_keys;
std::transform(begin(view), end(view), std::back_inserter(doc_keys), [](document::element ele) {
// note that key() returns a string_view
return ele.key().to_string();
});
std::cout << "document keys are: " << std::endl;
for (auto key : doc_keys) {
std::cout << key << " " << std::endl;
}
std::cout << std::endl;
}
示例13: AmfByteArray
AmfByteArray(const T& v) {
using std::begin;
using std::end;
value = std::vector<u8>(begin(v), end(v));
}
示例14: c
TEST(FilterTest, ConstructRangeSlotsHash) {
const int elems[] = {2, 3, 4, 5, 1, 2, 3};
const filter_t c(begin(elems), end(elems), 30, test_hash{29});
expect_properties(c, sc_at_least(32), test_hash{29}, default_max_load_factor);
expect_contents(c, {1, 2, 3, 4, 5});
}
示例15: main
int main()
{
#ifdef LIST_INIT
// list initialization, articles has 3 elements
vector<string> articles = {"a", "an", "the"};
#else
string temp[] = {"a", "an", "the"};
vector<string> articles(begin(temp), end(temp));
#endif
vector<string> svec; // default initialization; svec has no elements
vector<int> ivec; // ivec holds objects of type int
vector<Sales_item> Sales_vec; // holds Sales_items
vector<vector<string>> file; // vector whose elements are vectors
vector<vector<int>> vecOfvec; // each element is itself a vector
// all five vectors have size 0
cout << svec.size() << " " << ivec.size() << " "
<< Sales_vec.size() << " "
<< file.size() << " " << vecOfvec.size() << endl;
vector<int> ivec2(10); // ten elements, each initialized to 0
vector<int> ivec3(10, -1); // ten int elements, each initialized to -1
vector<string> svec2(10); // ten elements, each an empty string
vector<string> svec3(10, "hi!"); // ten strings; each element is "hi!"
cout << ivec2.size() << " " << ivec3.size() << " "
<< svec2.size() << " " << svec3.size() << endl;
// 10 is not a string, so cannot be list initialization
vector<string> v1(10); // construct v1 with ten value-initialized elements
#ifdef LIST_INIT
vector<string> v2 {10}; // ten elements value-initialized elements
#else
vector<string> v2(10);
#endif
vector<string> v3(10, "hi"); // ten elements with value "hi"
#ifdef LIST_INIT
// again list initialization is not viable, so ordinary construction
vector<string> v4{10, "hi"}; // ten elements with values "hi"
#else
vector<string> v4(10, "hi"); // ten elements with values "hi"
#endif
// all four vectors have size ten
cout << v1.size() << " " << v2.size()
<< " " << v3.size() << " " << v4.size() << endl;
#ifdef LIST_INIT
vector<string> vs1 {"hi"}; // list initialization: vs1 has 1 element
vector<string> vs2{10}; // ten default-initialized elements
vector<string> vs3{10, "hi"}; // has ten elements with value "hi"
#else
vector<string> vs1;
vs1.push_back("hi"); // explicitly add the element; vs1 has 1 element
vector<string> vs2(10); // don't use curlies;
// vs2 has ten default-initialized elements
vector<string> vs3(10, "hi"); // don't use curlies;
// vs3 has ten elements with value "hi"
#endif
cout << vs1.size() << " " << vs2.size() << " " << vs3.size() << endl;
vector<int> v5(10, 1); // ten elements with value 1
#ifdef LIST_INIT
vector<int> v6 {10, 1}; // two elements with values 10 and 1
#else
vector<int> v6;
v6.push_back(10);
v6.push_back(1);
#endif
cout << v5.size() << " " << v6.size() << endl;
#ifdef LIST_INIT
// intention is clearer
vector<int> alt_v3 = {10}; // one element with value 10
vector<int> alt_v4 = {10, 1}; // two elements with values 10 and 1
#else
vector<int> alt_v3;
alt_v3.push_back(10); // one element with value 10
vector<int> alt_v4;
alt_v4.push_back(10);
alt_v4.push_back(1); // two elements with values 10 and 1
#endif
cout << alt_v3.size() << " " << alt_v4.size() << endl;
return 0;
}