#include <vector>
#include <map>
#include <string>
#include <list>
#include <charconv>
#include <cstring>
template <typename T>
class resizing_vector : public std::vector<T> {
public:
using std::vector<T>::vector;
T& operator[](std::size_t index) {
if (index >= std::vector<T>::size()) {
std::vector<T>::resize(index + 1);
}
return std::vector<T>::operator[](index);
}
};
std::list<std::string> g_symbols;
std::map<std::string_view, resizing_vector<std::string> > g_cache;
int
main()
{
const char *msg = "|5=AAPL|270=some_value";
std::string_view key(msg+3,4);
auto iter = g_cache.find(key);
if( iter == g_cache.end())
{
g_symbols.emplace_back(key);
iter = g_cache.insert( std::make_pair(g_symbols.back(), 0) ).first;
}
auto &tag_values = iter->second;
// do this for each tag in message
int tag = 0;
std::from_chars(msg+8,msg+11, tag);
auto &value = tag_values[tag];
value.resize(10);
memcpy(value.data(),msg+12,10);
// dump the cache
for(auto &[key,tag_values] : g_cache)
{
printf("key: %.*s\n", (int)key.size(), key.data());
for(int i=0;i<tag_values.size(); i++)
if(tag_values[i] != "")
printf("\ttag: %d, value: %s\n", i, tag_values[i].c_str());
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: