|
int main ()
{
vector <string> svec;
string str;
// enter the vector element
cout << "Enter strings: (Ctrl + Z to end)" << endl;
while (cin >> str)
svec.push_back (str);
// create character pointer array
char ** parr = new char * [svec.size ()];
// handle vector elements
size_t ix = 0;
for (vector <string> :: iterator iter = svec.begin ();
iter! = svec.end (); ++ iter, ++ ix) {
// create character array
char * p = new char [(* iter) .size () + 1]; // String type supports .size () method
// Copy the data of the vector element to the character array
strcpy (p, (* iter) .c_str ());
// insert a pointer to the character array into the character pointer array
parr [ix] = p;
}
// Output the contents of the vector object
cout << "Content of vector:" << endl;
for (vector <string> :: iterator iter2 = svec.begin ();
iter2! = svec.end (); ++ iter2)
cout << * iter2 << endl; // Iterative output needs to be dereferenced
// print the contents of the character array
cout << "Content of character arrays:" << endl;
for (ix = 0; ix! = svec.size (); ++ ix)
cout << parr [ix] << endl; //? ? ? Outputting a pointer means outputting what this pointer points to? ? ? ? ? ? ?
// release each character array
for (ix = 0; ix! = svec.size (); ++ ix)
delete [] parr [ix];
// release the character pointer array
delete [] parr;
return 0;
}
The code is as above. The question mark is the problem. If I enter the string: a, b, c, CtrlZ, and then output the Content of character arrays, the output is parr [ix]. But under VS2005, it outputs a, b, c, and if you change the code to * parr [ix], it still outputs a, b, c, which is very unclear. The first output should be a pointer, not a string Ah? Expert solution |
|