本文整理匯總了C++中std::accumulate方法的典型用法代碼示例。如果您正苦於以下問題:C++ std::accumulate方法的具體用法?C++ std::accumulate怎麽用?C++ std::accumulate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類std
的用法示例。
在下文中一共展示了std::accumulate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。
示例1: main
int main() {
vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8};
int result = accumulate(vec.cbegin(), vec.cend(), 0);
cout << result << endl;
vector<double> doubleVec{1.11, 2.22, 3.33};
auto res = accumulate(doubleVec.cbegin(), doubleVec.cend(), 0); // lost precision due to the third argument, function returns a int instead of double
cout << res << endl;
return 0;
}
示例2: main
int main()
{
vector<int>ivec(10); //default initialized to 0
fill(ivec.begin(), ivec.end(), 1); //reset each element to 1
//print elements in ivec
auto iter = ivec.begin();
while (iter != ivec.end())
cout << *iter++ << " ";
cout << endl;
//sum the elements in ivec starting the summation with the value 0
int sum = accumulate(ivec.begin(), ivec.end(), 0);
cout << sum << endl;
//set a subsequence of the container to 10
fill(ivec.begin(), ivec.begin() + ivec.size() / 2, 10);
cout << accumulate(ivec.begin(), ivec.end(), 0) << endl;
//reset the same subsequence to 0
fill_n(ivec.begin(), ivec.size() / 2, 0);
cout << accumulate(ivec.begin(), ivec.end(), 0) << endl;
//concatenates elements in a vector of strings and store in sum
vector<string>v;
string s;
while (cin >> s)
v.push_back(s);
string concat = accumulate(v.begin(), v.end(), string(""));
cout << concat << endl;
getchar();
}
示例3: main
int main()
{
vector<int> vec(10); // default initialized to 0
fill(vec.begin(), vec.end(), 1); // reset each element to 1
// sum the elements in vec starting the summation with the value 0
int sum = accumulate(vec.cbegin(), vec.cend(), 0);
cout << sum << endl;
// set a subsequence of the container to 10
fill(vec.begin(), vec.begin() + vec.size()/2, 10);
cout << accumulate(vec.begin(), vec.end(), 0) << endl;
// reset the same subsequence to 0
fill_n(vec.begin(), vec.size()/2, 0);
cout << accumulate(vec.begin(), vec.end(), 0) << endl;
// create 10 elements on the end of vec each with the value 42
fill_n(back_inserter(vec), 10, 42);
cout << accumulate(vec.begin(), vec.end(), 0) << endl;
// concatenate elements in a vector of strings and store in sum
vector<string> v;
string s;
while (cin >> s)
v.push_back(s);
string concat = accumulate(v.cbegin(), v.cend(), string(""));
cout << concat << endl;
return 0;
}
示例4: random_employe_per_weight
int AlgoRecuit::random_employe_per_weight(ecosystem_sol_t * eco, bool min_or_not_max) {
// vector of <employe_id, sum of hours>
vector<pair<int,int>> sums;
int total_sum = 0;
int current_sum = 0;
for(auto i: (*eco)){
current_sum = accumulate(i.second.begin(), i.second.end(), 0);
sums.push_back(pair<int, int>(i.first, current_sum));
total_sum += current_sum;
}
vector<float> weights;
if(min_or_not_max) {
for(auto sum: sums){
weights.push_back( (float)(total_sum - sum.second) / (float)total_sum);
}
} else {
for(auto sum: sums){
weights.push_back((float)sum.second / (float)total_sum);
}
}
discrete_distribution<> dist(weights.begin(), weights.end());
mt19937 gen(random_device{}());
auto employe = sums[dist(gen)];
return employe.first;
}
示例5: main
int main(int argc, char** argv)
{
GetPot args(argc, argv);
int nthreads = args.follow(1 , "--threads");
int samp = args.follow(100, "--samp");
int reps = args.follow(1 , "--reps");
double shape = args.follow(1.0, "--shape");
double tilt = args.follow(0.0, "--tilt");
fprintf(stderr, "Will draw %i samples %i times using %i threads.\n", samp, reps, nthreads);
fprintf(stderr, "Shape: %g; Tilt: %g\n", shape, tilt);
vector<double> x(samp);
vector<double> h(samp);
vector<double> z(samp);
fill(h.begin(), h.end(), shape);
fill(z.begin(), z.end(), tilt);
vector<RNG> r(nthreads);
vector<PolyaGamma> dv(nthreads);
vector<PolyaGammaAlt> alt(nthreads);
vector<PolyaGammaSP> sp(nthreads);
struct timeval start, stop;
gettimeofday(&start, NULL);
for (int i = 0; i < reps; i++)
rpg_hybrid_par(&x[0], &h[0], &z[0], &samp, &nthreads, &r, &dv, &alt, &sp);
gettimeofday(&stop, NULL);
double diff = calculateSeconds(start, stop);
fprintf(stderr, "Time: %f sec. for %i samp (%i times)\n", diff, samp, reps);
// Check output
double m1_hat = accumulate(x.begin(), x.end(), 0.0) / samp;
double m2_hat = accumulate(x.begin(), x.end(), 0.0, addsq) / samp;
fprintf(stderr, "Sample moments: m1: %g; m2: %g\n", m1_hat, m2_hat);
fprintf(stderr, "Actual moments: m1: %g, m2: %g\n", dv[0].pg_m1(shape, tilt), dv[0].pg_m2(shape, tilt));
}
示例6: log
/**
* \brief TODO
*/
double
RegressionModel::loglikelihood(const vector<double>& responses,
const vector<double>& probs,
const vector< vector<double> >& covars) const {
double ll = 0;
for (size_t i=0; i<responses.size(); i++) {
ll += (this->loglikelihood(responses[i], covars[i]) + log(probs[i]));
}
return ll - log(accumulate(probs.begin(), probs.end(), 0.0));
}
示例7: eval
inline static typename field<Vector>::type eval(Vector const & v, typename field<Vector>::type const & init_value, BinaryFunctor binary_op)
{
using std::accumulate;
return accumulate(
v.begin(),
v.end(),
init_value,binary_op
);
}
示例8: random_animal_per_weight
int AlgoRecuit::random_animal_per_weight(vector<int> * tasks) {
int sum = accumulate(tasks->begin(), tasks->end(), 0);
vector<float> weights;
for(auto w: *tasks){
weights.push_back((float)w/(float)sum);
}
discrete_distribution<> dist(weights.begin(), weights.end());
mt19937 gen(random_device{}());
return dist(gen);
}
示例9: dirdisc_lpdf
double dirdisc_lpdf(const vector<double> &cts, double alpha)
{
double n = accumulate(cts.begin(), cts.end(), 0);
double k = static_cast<double>(cts.size());
double sum_a = alpha*k;
double prd_xa = 0;
for (const auto &ct : cts)
prd_xa += lgamma(ct + alpha);
return lgamma(sum_a) - lgamma(n+sum_a) + prd_xa - k*lgamma(alpha);
}
示例10: accumulate
FileSplit::FileSplit ( const IntVector& nFileSpec, const DoubleVector& prop, int maxSpectra )
{
totSpec = accumulate ( nFileSpec.begin (), nFileSpec.end (), 0 );
int maxSpecPerProcess = static_cast<int> ( totSpec * prop [0] );
numSerial = ( maxSpecPerProcess / maxSpectra ) + 1; // Number of serial searches
IntVector nSearchSpec;
for ( DoubleVectorSizeType i = 0 ; i < prop.size () ; i++ ) {
for ( int j = 0 ; j < numSerial ; j++ ) {
nSearchSpec.push_back ( static_cast<int> ( ( totSpec / numSerial ) * prop [i] ) );
}
}
int rem = accumulate ( nSearchSpec.begin (), nSearchSpec.end (), 0 ) - totSpec;
while ( rem != 0 ) {
int inc = ( rem < 0 ) ? 1 : -1;
for ( DoubleVectorSizeType i = 0 ; i < prop.size () ; i++ ) {
nSearchSpec [i] += inc;
rem += inc;
if ( rem == 0 ) break;
}
}
init ( nFileSpec, nSearchSpec );
}
示例11: join
QString join(const FSAMorphemeList& v, const QString& delim)
{
using linguistica::join_helper::cat_with_delim;
using std::accumulate;
FSAMorphemeList::const_iterator iter = v.constBegin();
if (iter == v.constEnd())
// empty list
return QString();
const QString first = (*iter)->toStr();
return accumulate(++iter, v.constEnd(), first, cat_with_delim(delim));
}
示例12: accumulate
void
add_sequencing_errors(const vector<vector<double> > &errors,
vector<vector<double> > &prb) {
for (size_t i = 0; i < prb.size(); ++i) {
size_t base = max_element(prb[i].begin(), prb[i].end()) - prb[i].begin();
const double sum = accumulate(errors[i].begin(), errors[i].end(), 0.0);
prb[i][base] -= sum;
prb[i][base] = std::max(0.0, prb[i][base]);
transform(prb[i].begin(), prb[i].end(),
errors[i].begin(), prb[i].begin(),
std::plus<double>());
}
}
示例13: dirmul_lpdf
// ---
double dirmul_lpdf(const vector<size_t> &cts, double alpha)
{
double n = static_cast<double>(accumulate(cts.begin(), cts.end(), 0));
double k = static_cast<double>(cts.size());
double sum_a = alpha*k;
double prd_xa = 0;
double prd_x1 = 0;
for (const auto &ct : cts){
prd_xa += lgamma(ct + alpha);
prd_x1 += lgamma(ct + 1.);
}
return lgamma(sum_a) + lgamma(n+1) - lgamma(n+sum_a) - prd_x1 +\
prd_xa - k*lgamma(alpha);
}
示例14: pow
template <> float calculateGBCEAllShareIndex<float>::operator() (const vector<float>& prices) {
if (prices.size() == 0) {
return 0.0f;
}
float PriceSum = accumulate(prices.begin(), prices.end(), 1.0f,
[] (float sum, const float& entry) {
return sum * entry;
});
if (PriceSum == 0) {
return 0.0f;
}
float vectorLength = (float)prices.size();
float powerToRaiseTo = 1.0 / vectorLength;
return pow(PriceSum, powerToRaiseTo);
}
示例15: while
void
add_sequencing_errors(const Runif &rng, const double max_errors,
string &seq, vector<vector<double> > &quality_scores) {
// first make the pwm:
quality_scores.resize(seq.length(), vector<double>(smithlab::alphabet_size, 0.0));
for (size_t i = 0; i < seq.length(); ++i)
quality_scores[i][base2int(seq[i])] = 1.0;
double total_error = max_errors;
while (total_error > 0) {
// sample an error position:
const size_t error_pos = rng.runif(0ul, seq.length());
// sample an error amount:
double remaining_freq = min(quality_scores[error_pos][base2int(seq[error_pos])],
total_error);
const double error_amount = min(min(rng.runif(0.0, 1.0), max_errors),
remaining_freq);
size_t error_base = base2int(seq[error_pos]);
while (error_base == base2int(seq[error_pos]))
error_base = rng.runif(0ul, smithlab::alphabet_size);
quality_scores[error_pos][base2int(seq[error_pos])] -= error_amount;
quality_scores[error_pos][error_base] += error_amount;
total_error -= error_amount;
}
for (size_t i = 0; i < quality_scores.size(); ++i) {
std::transform(quality_scores[i].begin(), quality_scores[i].end(),
quality_scores[i].begin(), std::bind2nd(std::plus<double>(), 1e-3));
const double column_sum = accumulate(quality_scores[i].begin(),
quality_scores[i].end(), 0.0);
std::transform(quality_scores[i].begin(), quality_scores[i].end(),
quality_scores[i].begin(), std::bind2nd(std::divides<double>(), column_sum));
}
for (size_t i = 0; i < quality_scores.size(); ++i)
for (size_t j = 0; j < smithlab::alphabet_size; ++j)
quality_scores[i][j] = 10*(log(quality_scores[i][j]) -
log(1 - quality_scores[i][j]))/log(10);
}