HelpMe

what is the use of static function in c++ and when we use the static fucntion
Last edited on
myint is a char array that you use for all strings. That is OK but if you want to store previous entries, you must create copies of those entries. The easiest would be to use a vector of string objects, std::vector<std::string> myvector;.
instead vector<char *> myvector;
use just
vector<char > myvector;
1.In the first option my output is satisfied but why can't come on the char * please tell me the reason.

2.In the second option only holds the single character but i want a string .
You are reusing the myint[] char array to receive input repeatedly. This is ONE string. It cannot hold TWO strings. When you push_back() a pointer to a char, you are just pushing a memory address. You are NOT producing copies of the actual string. Therefore, all items in the vector of type char* point to the same string always, which is myint.

If you use a vector of std::string objects, C++ automatically constructs those objects in the call to push_back(), and this effectively copies the string into a new memory address. This means you are saving the data and now you can use the original char array for whatever.
Why are the first 3 reported?
I think you wanna do something like this:

// vector::push_back
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main ()
{
vector<char *> myvector;
//char myint[22];
int n=2;
//look at hera
char (*myint)[22];
myint = new char[n][22];
//end
cout << "Please enter some integers (enter 0 to end):\n";

do
{
n--;
//mind the range
cin >> myint[n];
myvector.push_back (myint[n]);
} while (n > 0);

cout << "myvector stores " << (int) myvector.size() << " numbers.\n";
vector<char*>::iterator it;
for(it=myvector.begin();it!=myvector.end();it++)
{
cout<<*it<<endl;
}
//release
delete[] myint;
return 0;
}
Topic archived. No new replies allowed.