Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing data of unknown size in C++

I've been using PHP for about 4 years, however I've come across a problem that requires something with slightly (:P) better performance and so I've chosen C++.

The program I'm writing is a Linux daemon that will scan a MySQL database for URLs to load, load them using cURL, search for a specified string, and then update the database accordingly. The problem that I'm facing is that I don't know the size of the data that needs to be stored in a variable in order to be searched for a specific string.

I had the idea of using a linked list and allocating more nodes as the data fills the list. Is this a good way to go about doing things?

Thanks in advance,

like image 741
Kewley Avatar asked Jan 23 '23 01:01

Kewley


2 Answers

in c++ the vector class can store an unknown sized amount of data.

#include <string>
#include <vector>

std::vector <std::string>Data;

std::string newData = "a String";
Data.push_back(newData);

std::string otherData = "a different String";
Data.push_back(otherData);

of course 'string' could be any data type you want, and you can access the data using the Data[0] to return the first string, and you can use Data.size() to return the amount of strings in the vector/array.

for(int x = 0; x != Data.size(); x++)
{
   //Do what you want with the data here using Data[x]
}
like image 57
Sam Avatar answered Feb 04 '23 09:02

Sam


In general, you would want to use one of the standard containers. My personal suggestion is std::vector. YOu can use it as an array (as the data is guaranteed to be contiguous) and it has convenient indexing and insertion operations (seems like you aren't interested in removal at this point).

Specifically, you could set up something like

std::vector<char> buff;
// while you are reading data
buff.push_back (item);

When you are done you can call buff.size to find out how much you read.

As an added bonus (if you are in fact dealing with character buffers), when you finally get all the data you need you can convert to a std::string to do whatever searching you want to do.

std::vector<char> buff;
buff.push_back('e');
buff.push_back('a');
buff.push_back('t');

std::string s(&buff[0], buff.size());

Edited for correctness.

like image 24
ezpz Avatar answered Feb 04 '23 10:02

ezpz