|
-
December 30th, 2002, 09:48 AM
#1
about string arrays
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.
-
December 30th, 2002, 10:26 AM
#2
Try this out...
indicolifa,
Try this simple code snippet. The sample program uses new to create an array of 16 strings with 128 characters each.
Chris.
Code:
#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;
}
You're gonna go blind staring into that box all day.
-
December 30th, 2002, 10:48 AM
#3
Or....
Code:
#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];
}
}
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
December 30th, 2002, 11:04 AM
#4
thanks... vector question
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
-
December 30th, 2002, 11:15 AM
#5
thank you and two more questions, people!
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.
-
December 30th, 2002, 11:23 AM
#6
STL
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.
You're gonna go blind staring into that box all day.
-
December 30th, 2002, 11:31 AM
#7
MMX, SSE
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.
You're gonna go blind staring into that box all day.
-
December 30th, 2002, 01:45 PM
#8
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
Code:
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:
Code:
#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, or a more complete guide here
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|