Click here to Skip to main content
15,849,137 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: RegQueryValueExW vs RegQueryValueExA Pin
Dave Kreskowiak18-Jun-23 5:42
mveDave Kreskowiak18-Jun-23 5:42 
Questionmutex problem Pin
PV202314-Jun-23 21:51
PV202314-Jun-23 21:51 
AnswerRe: mutex problem Pin
Richard MacCutchan14-Jun-23 22:17
mveRichard MacCutchan14-Jun-23 22:17 
AnswerRe: mutex problem Pin
jschell15-Jun-23 3:33
jschell15-Jun-23 3:33 
QuestionIs there a way to add all the elements of a vector to an integer sequentially? Pin
puckettrobinson6755-Jun-23 19:16
puckettrobinson6755-Jun-23 19:16 
AnswerRe: Is there a way to add all the elements of a vector to an integer sequentially? Pin
Victor Nijegorodov5-Jun-23 21:39
Victor Nijegorodov5-Jun-23 21:39 
AnswerRe: Is there a way to add all the elements of a vector to an integer sequentially? Pin
Richard MacCutchan5-Jun-23 22:30
mveRichard MacCutchan5-Jun-23 22:30 
AnswerRe: Is there a way to add all the elements of a vector to an integer sequentially? Pin
CPallini5-Jun-23 22:44
mveCPallini5-Jun-23 22:44 
Quote:
Is there a way to add all the elements of a vector to an integer sequentially?
Yes, there are, at least, three different ways:
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


void dump (vector<int> v)
{
  for (auto x :v )
    cout << x << " ";
  cout << "\n";
}

int main()
{

  { // method 1: access items by indices
    vector<int> v{1,2,3};

    int c = 4;

    for (size_t n = 0; n<v.size(); ++n)
    {
      v[n] += c;
    }

    cout << "method 1: ";
    dump(v);
  }

  { // method 2: access items with ranged for loop
    vector<int> v{1,2,3};

    int c = 4;

    for (auto & x : v)
    {
      x += c;
    }

    cout << "method 2: ";
    dump(v);
  }


  { // method 3: use 'transform' of the algorithm library
    vector<int> v{1,2,3};

    int c = 4;

    transform(v.begin(), v.end(), v.begin(), [=](int  x){ return x+c; });

    cout << "method 3: ";
    dump(v);
  }
}


Quote:
As a side note, is there a way to add a repeating number to an integer to infinity?
Yes, but this way the addition will overflow. Try:
C++
#include <iostream>
using namespace std;


int main()
{
  int i = 1;
  int last_i;
  int c = 4;
  for (;;)
  {
    last_i = i;
    i += c;
    if ( i < last_i)
      break;
  }

  cout <<  "unfortunately, (" << last_i << "+" << c << ") makes " <<  i;
}

"In testa che avete, Signor di Ceprano?"
-- Rigoletto

QuestionImage compression Pin
Calin Negru30-May-23 10:09
Calin Negru30-May-23 10:09 
AnswerRe: Image compression Pin
OriginalGriff30-May-23 10:18
mveOriginalGriff30-May-23 10:18 
GeneralRe: Image compression Pin
Calin Negru31-May-23 0:52
Calin Negru31-May-23 0:52 
QuestionCom Automation latest library for working with ms excel. Pin
I_am_nayak22-May-23 19:34
I_am_nayak22-May-23 19:34 
AnswerRe: Com Automation latest library for working with ms excel. Pin
_Flaviu22-May-23 21:28
_Flaviu22-May-23 21:28 
AnswerRe: Com Automation latest library for working with ms excel. Pin
Victor Nijegorodov22-May-23 23:47
Victor Nijegorodov22-May-23 23:47 
QuestionCPU Usage Pin
Richard Andrew x6419-May-23 16:56
professionalRichard Andrew x6419-May-23 16:56 
AnswerRe: CPU Usage Pin
Dave Kreskowiak19-May-23 18:48
mveDave Kreskowiak19-May-23 18:48 
GeneralRe: CPU Usage Pin
harold aptroot19-May-23 19:53
harold aptroot19-May-23 19:53 
AnswerRe: CPU Usage Pin
Greg Utas20-May-23 1:13
professionalGreg Utas20-May-23 1:13 
AnswerRe: CPU Usage Pin
Gerry Schmitz20-May-23 5:04
mveGerry Schmitz20-May-23 5:04 
AnswerRe: CPU Usage Pin
Randor 20-May-23 7:38
professional Randor 20-May-23 7:38 
GeneralRe: CPU Usage Pin
Richard Andrew x6420-May-23 11:21
professionalRichard Andrew x6420-May-23 11:21 
GeneralRe: CPU Usage Pin
Dave Kreskowiak20-May-23 20:30
mveDave Kreskowiak20-May-23 20:30 
QuestionRe: CPU Usage Pin
David Crow22-May-23 8:49
David Crow22-May-23 8:49 
AnswerRe: CPU Usage Pin
Dave Kreskowiak22-May-23 9:43
mveDave Kreskowiak22-May-23 9:43 
GeneralRe: CPU Usage Pin
Dave Kreskowiak20-May-23 11:28
mveDave Kreskowiak20-May-23 11:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.