views:

439

answers:

4

Hi,

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 URL's 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,

A: 

What string you will search for and what update you will make after search based on the search string? Please throw some light on it.

Xolve
This should be a comment.
Salgar
+2  A: 

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.

ezpz
For the string conversion, do not forget to push a '\0'!
RaphaelSP
In the second snippet it will output 'eat' only if you get lucky. C-style strings, which the vector holds, need to be null-terminated.
UncleBens
@Raphael: or you could use the string contructor which takes both a char * and a size. Something like: `std::string s(`
Evan Teran
Right, it will also fail when null characters appear in the stream itself. Providing a size in the constructor helps this, but does not eliminate the problem. I'll update to be explicit.
ezpz
@Evan: Oops. My brain erased the second parameter.
RaphaelSP
-1 This answer has the nice suggestion of using std::vector and then it goes so wrong when going back to a std::string!!! It would be simpler constructing with `std::string s( v.begin(), v.end() )`, there is no need to push a null character at the end and the string will take as many characters as there are in the buffer (both with the iterator or the `const char*`, size constructors) regardless of whether there is a null (0) character in the middle of the buffer.
David Rodríguez - dribeas
Thanks for the info.
Kewley
+2  A: 

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]
}
Sam
You need to use fully-qualified names or `using namespace std;`
James McNellis
Thanks James, I was just editing it to put that in there :)
Sam
Thanks for the info, I'll give it a go in a bit.
Kewley
You would be better off using iteration (with const_iterator), it's more resilient to change in favor of another container.
Matthieu M.
A: 

You have a whole lot to discover there.

You should specifically orient your discovery to the STL: use C++ reference

Now you should try to learn how to use:

  • std::vector
  • std::string
  • std::cin and std::cout (globals)

On std::string you should note a good number of algorithm like find, find_first_of, find_last_of.

Note that string manipulations in C++ can be quite challenging (as in verbose).

If you are comfortable with regular expressions, you might be willing to try Boost.Regex. Note that you have to link with its library.

Also, if you come from PHP and want to increase your performance, you could begin with a lot of different scripting languages (Python is my favorite), and it would probably be easier.

Matthieu M.
C/C++ is something I've always wanted to learn more about so I thought I might as well just jump in the deep end and write an app. I've never really been able to like Python that much. Thanks for the tips!
Kewley
Good luck then, I personally have fun with the language :)
Matthieu M.