searching in array..
hi friends..
i have an array whith 22306 variable in it..chars..dinamic array
not i'd search a name by the array..
char *array;
array = new char [length];
...
delete [] array;
now there are a structure of array same this:
array[1] = c
array[2] = o
array[3] = d
array[4] = e
array[5] = g
array[6] = u
array[7] = r
array[8] = u
i would show only code...
but i don't use for stratmente , becouse the word "code"
isn't in the same position..
i have the text in a file, and whith fstream, i convert it in an array, so
if i change text, the program must verify in whot position code is,
and how long is it "code" = 4
how can i do professionally??
thnaks!!...
[799 byte] By [
Rooting] at [2007-11-20 11:38:46]

# 2 Re: searching in array..
yeah is the same..
but to convert array whith string, can i use the template
by philip nicoletti??
template <typename T1, typename T2>
T2 Convert(const T1 & t1)
{
stringstream ss;
ss << t1;
T2 t2 = T2();
ss >> t2;
return t2;
}
there convert in a string an array, so is right..isn't it??
and whot i do in [] of array??..
Convert<char, string>(array[??])
# 3 Re: searching in array..
std::string has constructors that can used. If your array of char is actually a null terminated string, you can just write:
std::string str(array);
If it is not null terminated, then you can write:
std::string str(array, array + length);
# 4 Re: searching in array..
to take the data i use the function
is.open ("file.dat", ios::binary );
is.read (buffer,length);
and read function has a costructor whith a char not string..
so i convert it...
# 7 Re: searching in array..
ohh thanks!!..
I resolve it whith str.find...
to show whot position are the word i use
found=str.find(find);
and call it whith int(found)
but if not exist whot i do??..
# 8 Re: searching in array..
for string::find() the function returns string::npos if not found ...
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
char pstr[] = "this is a test : dev-archive";
const int length = 35;
string str(pstr,length);
size_t pos = -1;
char * ps = strstr(pstr,"code");
if (ps != 0) // found
{
pos = ps - pstr;
cout << "position using char array = " << pos << "\n";
}
pos = str.find("code");
if (pos != string::npos) // found
{
cout << "position using std::string = " << pos << "\n";
}
return 0;
}