trying to avoid repition

I'm trying to create a way to store information where the most recent data has a greater influence. This what I have thought of so far. I stopped at 5, but it goes on and on, is there an easier/more efficient way to program this information?

1
2
3
4
5
6
7
8
9
string vocabulary[1000];
cin >> vocabulary[0];

vocabulary[5] = vocabulary[4];
vocabulary[4] = vocabulary[3];
vocabulary[3] = vocabulary[2];
vocabulary[2] = vocabulary[1];
vocabulary[1] = vocabulary[0];
Last edited on
What does "greater influence" mean?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <vector>
#include <iostream>

int main() {
  std::vector<std::string> words;
  words.reserve( 1000 ); // can store 1000 words before reallocation is needed

  std::string input;
  while ( std::cin >> input ) {
    words.push_back( input );
  }

  if ( words.empty() ) return 1; // got no data

  std::cout << "Most recent: " << words.back() << '\n';

  auto rit = words.crbegin();
  for ( ; rit!= words.crend(); ++rit ) std::cout << *rit << ' ';
  std::cout << '\n';

  return 0;
}
are you asking about AI/sensor/etc decision algorithms, eg

sum = current *.75 + last *.10 + old *.05
...
if (sum > threshold) //threshold might be a sigmoid or something

... this is a 'weighted' approach where the current is worth a lot more than the previous value which is worth more than the value 2 iterations ago... a common technique if the data is numerical or can be treated as such...


another way is pure choice...
if current vs last does not make sense
use last
else use current (you can nest this to check against several historical values as needed, 2-3 is usual)

this is useful for like sensor/streamed data, where you occasionally get a glitch, so if you had
100 110 115 500000 ... you can ignore the 500000 value as a glitch and revert to 115 for an iteration...

does either of these make sense for what you are doing??

you probably don't ever want to do this rotating copy logic like your snippet shows.
a circular buffer is a good approach for this type of data, this is a very easy to write data structure that can be written on top of a vector very easily. If you are asking how to avoid all that copying, this is what you asked. If you want to understand how to weight stuff, the above may be useful.
Last edited on
jonnin, you are right on the money, thank you so much
Topic archived. No new replies allowed.