Click to See Complete Forum and Search --> : about string arrays
indiocolifa
December 30th, 2002, 08:48 AM
How I create an array of a char array (that is, a string array) to contain a defined number of elements specified by the user.
TO be clear, this is what i want to do, in Basic syntax:
Dim num As Integer
INPUT "Enter the number of options", num
Redim OptionDesc(0 To num) As String
For i=0 to num
Print "enter option " + i + "description: "
Line Input OptionDesc(i);
Next i
This can be done with a multidimensional char[x][x] type?
And with pointers ?
Its possible to create the variable in runtime with the NEW operator based on the number of options entered at runtime by the user. How i can do this.
2) difference, if you can reply, between plain "cin" and "cin.getline"
Thank you in advance.
dude_1967
December 30th, 2002, 09:26 AM
indicolifa,
Try this simple code snippet. The sample program uses new to create an array of 16 strings with 128 characters each.
Chris.
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
enum sizes
{
arry_sz = 16,
elem_sz = 128
};
// Allocate an array of pointers
char** p_string_array = new char*[arry_sz];
int i;
try
{
for(i = 0; i < arry_sz; i++)
{
// Allocate pointer data
p_string_array[i] = new char[elem_sz];
::memset(p_string_array[i], 0, elem_sz * sizeof(char));
}
}
catch(...)
{
return 0;
}
// Ask user for a string
::std::cout << "enter string 1: ";
::std::cin >> p_string_array[0];
// Ask user for another string
::std::cout << "enter string 2: ";
::std::cin >> p_string_array[1];
::std::cout << p_string_array[0] << ::std::endl;
::std::cout << p_string_array[1] << ::std::endl;
// Free the memory
for(i = 0; i < arry_sz; i++)
{
delete [] p_string_array[i];
}
delete [] p_string_array;
return 1;
}
Graham
December 30th, 2002, 09:48 AM
Or....
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int num;
cout << "Enter the number of options: ";
cin >> num;
vector<string> OptionDesc(num, "");
for (int i = 0; i < num; ++i)
{
cout << "Enter option " << i << " description: ";
cin >> OptionDesc[i];
}
}
indiocolifa
December 30th, 2002, 10:04 AM
thank you!!
about the second snippet...
what is a vector?
is a data type?
can you give me some clue of it use, etc?
(or an http link explaning it )
thank you!!!!!!!!!!!!!!!!!!!1
indiocolifa
December 30th, 2002, 10:15 AM
Thank you for advice, i understand the first example,
but i donīt know, in the second example, what is the <vector> stuff.. itīs a data type? where i can find info about <vector>?
another question: how to optimize a C++ program automatically with MMX/SSE instructions under VC6 or VC7? I need assembly or the compiler optimizes by itself?
thanks.
;)
dude_1967
December 30th, 2002, 10:23 AM
indio,
This vector thing is part of the so-called C++ standard template library (STL), a library which features containers (including strings, vectors and lists), I/O mechanisms, algorithms as well as locales and more...
As Graham, myself and others will undoubtedly agree, effective use of the STL is a very powerful programming tool, saving countless amounts of development time.
Check it out.
Chris.
:)
dude_1967
December 30th, 2002, 10:31 AM
indio,
I forgot to respond to the optimization part of the question. VC 6/7 only have rudimentary support of architecture-specific optimizations while compiling. Albeit VC7 does have some intrinsic functions for support of SIMD2: See the data types __m128d and __m128i.
The Intel C++ compiler, which can plug into VC7, offers legitimate Intel CPU-specific optimizations. There is a 30-day cost-free demo version available for download from Intel.
Try to get the coding done in a clean and proper fashion first while using general optimization practices. In my opinion, only the last rounds of optimization should involve the utilization of architecture-specific features.
Chris.
:cool:
Graham
December 30th, 2002, 12:45 PM
The C++ STL provides facilities so that you hardly ever have to use arrays again. The vector is the most array-like container in the STL - basically, it's a dynamic array that grows to contain whatever you put in it.
The line
vector<string> OptionDesc(num, "");
declares a vector of strings (string is another STL type that replaces char[] or char*), and so is the STL equivalent of your array of char*. The bit after the variable name - (num, "") - makes the vector num items long (by default vectors have zero length until you add something to them), and each item is intialised with an empty string.
Actually, given the dynamic nature of vector<>, I would have done things slightly differently - rather than ask for a size at the outset, I'd just keep accepting strings up to some predefined terminator and simply append them to the vector:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> OptionDesc;
string temp_string;
int i = 0;
do
{
cout << "Enter option " << i << " description: ";
cin >> temp_string;
if (!temp_string.empty())
OptionDesc.append(temp_string);
} while (!temp_string.empty());
}
There's a newbie guide to STL here (http://www.xraylith.wisc.edu/~khan/software/stl/STL.newbie.html), or a more complete guide here (http://www.sgi.com/tech/stl/table_of_contents.html)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.