answersLogoWhite

0


Best Answer

A string is an array; an array of character data types (char or wchar_t). Therefore anything you can do with an array you can also do with a string.

C does not have a built-in type for either an array or a string, but C++ does. In C, the programmer was entirely responsible for managing the memory allocated to an array. Built-in functions allowed us to determine the length of a null-terminated string, append strings, or alter the amount of memory allocated to an array, but the functions and the arrays were entirely separate. With C++, std::vector and std::string objects allow us to manipulate dynamic arrays and variable length strings, respectively, without having to worry about the underlying memory allocations. Since all the required methods are encapsulated within the objects themselves, they are much easier to work with.

For instance, in C, if we wanted to determine the length of a string we might call the strlen function:

char* str = "Hello world!";

unsigned size = strlen (str);

Arrays are a bit more difficult in that we must maintain a separate variable to keep track of an array's length:

unsigned size = 10;

int* arr = malloc(size);

size = 11;

arr = realloc(arr, size);

In C++, strings and arrays know their own length, so we don't need to call external functions or maintain separate variables:

std::string str = "Hello world!";

std::cout << '"' << str << '"' << " has " << str.size() << " characters\n";

std::vector<int> vect(10,0);

std::cout << "vect has " << vect.size() << " elements\n";

A vector of strings allows us to manipulate strings and vectors together. A vector of strings is essentially a two-dimensional dynamic array where each row is a string, and every string can have different lengths. The following therefore creates a vector with 10 empty strings:

std::vector< std::string > vstrings (10, "");

We can then manipulate each of the individual strings in the array:

vstrings[0] = "Hello world!";

vstrings[1] = "Another string.";

vstrings[2] = "The third string.";

...

vstrings[9] = "The string to end all strings.";

Each of these strings is a different length. With a traditional two-dimensional array each row would be the same length. In this case we'd need at least a 10 x 31 array (including null-terminators), which wastes memory when any string is less than 30 characters long. The only way to resolve this is to use a one-dimensional array of pointers to strings instead. Vectors of strings do the same thing, but they are much easier to work with since all memory management is handled by the objects themselves.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Manupulating strings and arrays together in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you swap two arrays in c plus plus using string?

You can't. While a string is a character array, an array is not necessarily a string. Treating arrays as if they were strings simply to swap them is madness. The correct way to physically swap arrays A and B is to copy A to a new array, C, then copy B to A, then C to B. If the arrays are the same size this is not a problem. If they are different sizes, you can only swap them if they are dynamic (not static). This means you must reallocate them. To speed up the process, copy the smallest array to C, first. A much better approach would be to point at the two arrays and swap the pointers instead.


C plus plus program that display student name course and grade using arrays of strings?

enum field { name, course, grade }; std::string student[3]; student[name] = "Joe Bloggs"; student[course] = "C++ Programming"; student[grade] = "A+";


What is the Difference between arrays in c and c plus plus?

Nothing whatsoever. They are exactly the same.


Array in c plus plus must be defined at compil time?

No. Arrays can be defined at runtime, just as they can in C. It's just that it's generally more convenient to use vectors instead of dynamic arrays at runtime, thus arrays are generally used statically, at compile time.


Difference between null and empty in c plus plus?

NULL is a constant with the value zero. It is typically used with pointers to signify the pointer is valid, but it does not store a valid memory address. In other words it points at nothing in particular. It is nullified. All pointers that are not currently in use must be nullified to signify the fact they are not in use. The term empty applies to arrays that have no elements: empty arrays. We also use the term when referring to empty strings. A string is simply an array of char, but while null-terminated strings always have at least one char, the null-terminator, the string itself is empty.


Do you have pointer concept in c plus plus language?

Yes. All string variables are pointers as are other arrays.


What procedure or operator allows you to extract a single character from a string in c plus plus?

Use the array index operator. Strings are just arrays of characters so use the zero-based index of the character you are interested in. Alternatively, use pointer arithmetic to achieve the same thing. Note that the string's name is a reference to the start of the character array.


Do you have to use delete operator on C plus plus strings before exiting your application?

no you dont


Is it possibly to return an array of strings in a function without using pointers in C plus plus?

No.


How can you get a specific string from large string using c plus plus strings?

std::string::substr();


A program for queue ADT by using arrays in c plus plus?

Arrays are not suitable for implementing queues because while they are ideal for adding to the end, the are not ideal for extraction from the beginning. For that you need a deque. Regardless, the STL (standard template library) already provides an efficient queue ADT in std::queue.


A c plus plus code to make a multidimensional array that can store strings?

int myarray=[5][5]; This snippet of code creates a 5X5 two-dimensional array. You can declare an array with more dimensions, but you shouldn't really need to go above more than four dimensions. Four-dimensional arrays are only used by high-end graphics programs or programs that need to calculate a ton of data.