banner



how to delete an element from an array c++

Member Avatar

Well, a simple way to achieve this would be to create another array, the same length as arr, go through each character, and if that character isn't a space then copy it to the second array. Like this.

              #include <iostream>  int main() {    char arr[] = "abc     def";     // Create another array the same length as arr    char temp[sizeof arr];     // Index of where to insert character in new array    int temp_index = 0;     // Loop through each character until null-terminator is reached    for (int i = 0; arr[i]; ++i) {       if (arr[i] != ' ') {          // Character isn't a space          temp[temp_index++] = arr[i];       }    }     // Add null-terminator    temp[temp_index] = '\0';     // Replace old array with new one    strcpy_s(arr, sizeof(arr), temp);     // Display the array    std::cout << arr;     // Pause    std::cin.ignore();    return 0; }            

But if you wanted to achieve this without the need of a second array, its going to require a more complex algorithm. Hope this helps, and I also hope I haven't given too much away :)

Member Avatar

you can use memmove() to shift everything left one place to overwrite the second consecutive space.

              char str[] = "Hello  World"; char* p = str; while( !isspace(*p) )    p++; if( isspace(*p) && isspace(*(p+1)) )    memmove(p+1, p+2, strlen(p+2) + 1); // + 1 to also move the NULL terminator            

Member Avatar

How about this, based on the idea of the bubble sort?

              void compact( char arr[], int size ) { 	int i, j; 	 	for( i = 1; i < size; i++ ) 	{ 		for( j = 1; j < size - i; j++ ) 		{ 			if( arr[j-1] == ' ' ) 			{ 				arr[j-1] = arr[j]; 				arr[j] = ' '; 			} 				 		} 	} }            

(yeah, I know there will be those of you deriding bubble sort in any form, but, it works!)

Member Avatar

You might need to revise this, as it will only remove one white-space.

That code must be put in a loop in order to remove other instances of multiple white spaces. I did not intend to post the entire solution, only a suggestion about how to go about solving it.

Not to mention the use of these tools for what should be simple strings is pretty dangerous!

memmove() is intended only for character arrays, not std::string. All C style string functions can be dangerous if the programmer uses them in a haphazard manner.

Member Avatar

ninwa's solution uses the same technique as mine, but using std::string's instead. Though it might be a better idea, it may overall be slower. Just to check, I did I little test.

ninwa's version:

              #include <iostream> using namespace std;  typedef unsigned __int64 uint64;  inline __declspec(naked) uint64 GetCycleCount() {     _asm rdtsc;     _asm ret; }  int main() {    uint64 cycle_timer_beg;    uint64 cycle_timer_end;     cycle_timer_beg = GetCycleCount();     for (int j = 0; j < 10000; ++j) {           char arr[] = {'a', 'b', 'c', ' ', ' ', ' ',                        ' ', ' ', 'd', 'e', 'f', '\0'};          string s(arr);          string tmp = "";          for (int i = 0; i < s.length(); i++) {             if (s.at(i) != ' ') {                tmp += s.at(i);             }          }          s = tmp;     }     cycle_timer_end = GetCycleCount();     cout << "Average cycle count: " << ((cycle_timer_end - cycle_timer_beg) / 10000);    cin.ignore();    return 0; }            

My version:

              #include <iostream> using namespace std;  typedef unsigned __int64 uint64;  inline __declspec(naked) uint64 GetCycleCount() {     _asm rdtsc;     _asm ret; }  int main() {    uint64 cycle_timer_beg;    uint64 cycle_timer_end;     cycle_timer_beg = GetCycleCount();     for (int j = 0; j < 10000; ++j) {        char arr[] = "abc     def";       char temp[sizeof arr];       int temp_index = 0;       for (int i = 0; arr[i]; ++i) {          if (arr[i] != ' ') {             temp[temp_index++] = arr[i];          }       }       temp[temp_index] = '\0';       strcpy_s(arr, sizeof(arr), temp);     }     cycle_timer_end = GetCycleCount();     cout << "Average cycle count: " << ((cycle_timer_end - cycle_timer_beg) / 10000);    cin.ignore();    return 0; }            

Hopefully this isn't going over the top with comparison, but I got the following output:

              ninwa's version. "Average cycle count: 917" My version.      "Average cycle count: 106"            

Thats over 8 times faster :) But ninwa's version I would say is more readable, even though it uses the same amount of code. Personally I prefer speed to readability, so I would stick with mine :icon_cheesygrin:

Member Avatar

Is that inline assembly code I see in both your programs? I guess you don't code for portability so that the code can be compiled by other people using other operating systems and/or compilers. And I'm sure a programming teacher would deduct points for that, unless its required as part of the assignment.

>>Personally I prefer speed to readability
Depends. Maintainability (and readability) are pretty important in the real-world.

Member Avatar

Portability isn't too important when it comes to making a quick speed test, as I woulden't be keeping it that way. Either way, if the code didn't compile for you, I posted my output :)

>Depends. Maintainability (and readability) are pretty important in the real-world.
Thats true

Member Avatar

If you want to remove mutliple characters from the array, you can simply change it like this.

              #include <iostream>  // Returns true if text contains ch inline bool contains(char *text, char ch) {    while (*text) {       if (*text++ == ch) {          return true;       }    }    return false; }  int main() {    char arr[] = "abc  \n   \t\ndef";    char spaces[] = " \t\n";     // Create another array the same length as arr    char temp[sizeof arr];     // Index of where to insert character in new array    int temp_index = 0;     // Loop through each character until null-terminator is reached    for (int i = 0; arr[i]; ++i) {       if (!contains(spaces, arr[i])) {          // Character isn't in the spaces array          temp[temp_index++] = arr[i];       }    }     // Add null-terminator    temp[temp_index] = '\0';     // Replace old array with new one    strcpy_s(arr, sizeof(arr), temp);     // Display the array    std::cout << arr; // Outputs "abcdef"     // Pause    std::cin.ignore();    return 0; }            

Now this will check to see if any of the char's in arr contain any of the char's in spaces, if it does, then it will remove them from the arr array.

Member Avatar

You people are making a mountain out of a molehill.

              int main() { char arr[] = {'a', 'b', 'c', ' ', ' ', ' ', ' ', ' ', 'd', 'e', 'f', '\0'}; char *p1, *p2;  p1 = strchr(arr,' '); // find 1st space p2 = strrchr(arr,' '); // find last space if(p1 && p2)    memmove(p1,p2+1,strlen(p2)+1); // remove all spaces cout << arr << "\n";  }            

how to delete an element from an array c++

Source: https://www.daniweb.com/programming/software-development/threads/147897/delete-or-remove-array-element

Posted by: wileylicep1943.blogspot.com

Related Posts

0 Response to "how to delete an element from an array c++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel