1

how do i insert <char> arrays into <char*> vectors something like this:

std::vector<char> ch;
ch.push_back('H');
ch.push_back('e');
ch.push_back('l');
ch.push_back('l');
ch.push_back('o');

std::vector<char*> vec;
vec.push_back(ch);

but it gives error saying : push_back() cannot convert parameter 1 from.... So is there a way to load ch[] values into vec[0] and still use one of my function which searches for the contents in the vector for example: vecf[] contains a file contents in it, i can search through the vecf[0]vecf[1]... for a similar content like searching a text "Hello" from "Hi, Hello..."?

As I was told my Question was unclear, So here is the clearer version: It is Possible to do like this:

char string[]="Hello World";
std::vector<char*> vec;
vec.push_back(string);
cout<<vec[0];

then if a string is of type std::vector like this:

std::vector<char> string;
std::vector<char*> vec;
how would i do like this?
vec.push_back(string);

or something like this to get the contents of string into vec, and ch and vec is not in the real project, i used the term ch and vec for just an example to how to do like this, in my project theres a char vector variable ID which is something like this ID="ID2" but in vector, the ID's values is loaded from a file, there is another tag which contains refering ID which i will compare with the ID[0]/ID[1] until one matches if one matched i will get its position on array, then theres another vector variable(int) which contains all the nodes position of array in the file, i will use like this SPos[ArrayPos] to get the position of the node in the file, hope this was clear enough...

  • 4
    Why don't you just use string in C++? – Pan Long Dec 07 '14 at 06:24
  • Why are you using `char*` in the first place? – n. m. could be an AI Dec 07 '14 at 06:27
  • It's indeed `std::string` that you are looking for, if you want to keep vector for some reason it's `int len = strlen(ch); std::vector vec; vec.insert(vec.begin,ch,ch+len);`. Oh and what failed for you was the conversion from `const char *` to `char *` – PeterT Dec 07 '14 at 06:28
  • @aga_pan Im more familiar with chars instead a string, and im using char* Because a can store values like "Hello world", a whole string can just be displayed as cout< which gives compiler error while doing like this: cout< contents into and still search for a specific content in them? – DeEp_FrEeze Dec 07 '14 at 06:34
  • @DeEp_FrEeze: No, that is not correct. Why do you think that? See a working example of printing a string here: http://ideone.com/kYTowC – Benjamin Lindley Dec 07 '14 at 06:37
  • @PeterT would'nt it give a compiler error because vec ch; is'nt a string? at most i recieved errors saying ch is a struct/class, and it was solved when i do like this ch[0], and the error was conversion from a struct/class to a char* – DeEp_FrEeze Dec 07 '14 at 06:40
  • `vec.push_back(ch.data())` except you won't be able to print it to `cout`, because your char array is not null-terminated. – Don Reba Dec 07 '14 at 06:40
  • @DeEp_FrEeze - I don't know where you're learning C++, but what you stated is all wrong concerning `std::string`. – PaulMcKenzie Dec 07 '14 at 06:46
  • I find your question very unclear, however it sounds like you need a `std::string` and not a `std::vector`. You can do all the `s[0];` style indexing on `std::string` as well as printing them out whole using `std::cout << s;`. – Galik Dec 07 '14 at 06:48
  • 1
    thanks @BenjaminLindley to refering me to ideone.com site, and i know what to do now, i will do like this in my project std::vector vec and this will store values like "Hello world", and actually im learning c++ at home, no teacher is here for teaching me, i learned c++ by my own but using another library i hates it, and i prefer using chars, but beause theres no way to do something like this vec.push_back("Hello World") automaticaly through vec.push_back(ch) i will use string instead, thanks for all your help especially Benjamin for helping me understand what a string is... – DeEp_FrEeze Dec 07 '14 at 07:02
  • Recommend you get a good book: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Galik Dec 07 '14 at 08:05

1 Answers1

-1
std::vector<char*> vec;

This says that vec is a vector of char*.

But you are trying to insert a vector. There is no automatic conversion possible here.

Assuming you really do not want to use string type, you would want to declare

std::vector<std::vector<char> > vec;
vec.push_back(ch);

Please note that, if you want pointers to vector to be stored, you have to have allocate ch on the heap.

KalyanS
  • 527
  • 3
  • 8
  • but it still gives errors like "no operator defined to take the right hand operand..." when doing cout< – DeEp_FrEeze Dec 07 '14 at 06:53
  • That is a slightly separate concern. vec[0] results in a vector. "cout << vec[0]" won't work as there is no such method. Based on your requirements it seems either you want to convert vector into a string or just use string type directly. – KalyanS Dec 07 '14 at 06:55