在C++中存储未知大小的数据

Kew*_*ley 4 c++ variables curl linked-list dynamic-memory-allocation

我已经使用PHP大约4年了,但是我遇到了一个问题需要稍微(:P)更好的性能,所以我选择了C++.

我正在编写的程序是一个Linux守护程序,它将扫描MySQL数据库以加载URL,使用cURL加载它们,搜索指定的字符串,然后相应地更新数据库.我面临的问题是我不知道需要存储在变量中的数据大小,以便搜索特定的字符串.

我想到了使用链表并在数据填充列表时分配更多节点.这是一个做事的好方法吗?

提前致谢,

Sam*_*Sam 6

在c ++中,vector类可以存储未知大小的数据量.

#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);
Run Code Online (Sandbox Code Playgroud)

当然'string'可以是你想要的任何数据类型,你可以使用Data [0]访问数据以返回第一个字符串,你可以使用Data.size()来返回vector中的字符串数量/阵列.

for(int x = 0; x != Data.size(); x++)
{
   //Do what you want with the data here using Data[x]
}
Run Code Online (Sandbox Code Playgroud)