Data Structure: an array of data structures

Source: Internet
Author: User

Data Structure: an array of data structures

Simple string operations are actually the same as those used by string library functions. However, this is a data storage method and operation, focuses on data storage.


The following is a non-Heap Storage operation of the string. It means that the white is stored in an array similar to that in the stack when memory is allocated:

/*************************************** * ******************************** Copyright (c) 2015, WK Studios ** Filename: str. h ** Compiler: GCC vc 6.0 ** Author: WK ** Time: 2015 25 5 ************************************* * *********************************/# pragma once # include <iostream> using namespace std; # include <assert. h> # define MAX_STR_LEN 20 typedef char SString [MAX_STR_LEN + 2]; // specify the length of the string for the first element, and then space '\ 0'. // 1. generate a string ssbool StrAssign (SString ss, char * s); // 2. copy string s to obtain string ssbool StrCopy (SString ss, SString s); // 1. returns the first address of the new string, char * my_strcpy (char * d, const char * str); // 3. empty bool StrEmpty (SString ss); // 4. comparison string int StrCompare (SString ss, SString s); // 2. string comparison size_t my_strcmp (const char * d, const char * str); // 5. evaluate the string length size_t StrLength (SString ss); // 3. evaluate the string length (when using the return value, you can force type conversion to convert unsigned to signed for calculation) size_t my_strlen (const char * str); // 6. concatenate string s1 and s2 into a new string ssbool Concat (SString ss, SString s1, SString s2); // 4. returns the first address of the new string, char * my_strcat (char * d, const char * str); // 7. returns the SubString of the string ss whose length is len starting from the pos position and saves bool SubString (SString sub, SString ss, size_t pos, size_t len) with array sub; // 8. returns the position where the same substring appears from the pos position of the ss string and the c string (only the same substring that appears for the first time is returned) size_t Index (SString ss, SString c, size_t pos); // 9. use string v to Replace the bool Replace (SString ss, SString c, SString v) that is equal to the c string in the ss string; // 10. insert the string cbool StrInsert (SString ss, size_t pos, SString c) before the pos character of the string ss; // 11. delete the string bool StrDelete (SString ss, size_t pos, size_t len) whose start length is len in the ss; // 12. destroy and delete the ssbool DestoryString (SString ss); // 13. display string void StrPrint (SString ss );

/*************************************** * ******************************** Copyright (c) 2015, WK Studios ** Filename: str. cpp ** Compiler: GCC vc 6.0 ** Author: WK ** Time: 2015 25 5 ************************************* * *********************************/# include "str. h "// 1. generate a string ssbool StrAssign (SString ss, char * s) {assert (strlen (s) <= MAX_STR_LEN ); // if the value is exceeded, ss [0] = strlen (s) cannot be placed; // the string length is placed on the first element for (int I = 1; I <= ss [0]; ++ I) {ss [I] = s [I-1];} ss [ss [0] + 1] = '\ 0'; // ensure that the' \ 0' with a string is returned true;} // 2. copy string s to get string ssbool StrCopy (SString ss, SString s) {// assert (ss [0]> s [0]); no need to judge, because the for (int I = 0; I <= s [0]; ++ I) {ss [I] = s [I];} has been determined during initialization. ss [ss [0] + 1] = '\ 0'; // ensure that the' \ 0' with a string is returned true;} // 1. returns the first address of the new string, char * my_strcpy (char * d, const char * str); // 3. empty bool StrEmpty (SString ss) {return ss [0] = 0;} // 4. comparison string int StrCompare (SString ss, SString S) {int I = 1; for (; I <= ss [0] & I <= s [0]; ++ I) {if (ss [I]! = S [I]) {return ss [I]-s [I] ;}} if (I> ss [0] | I> s [0]) // when the two strings are not of the same length, {return ss [I]-s [I];} return ss [0]-s [0]; // when the two strings are equal} // 2. string comparison size_t my_strcmp (const char * d, const char * str); // 5. evaluate the string length size_t StrLength (SString ss) {return ss [0];} // 3. evaluate the string length (when using the return value, you can force type conversion to convert unsigned to signed for calculation) size_t my_strlen (const char * str); // 6. concatenate string s1 and s2 into a new string ssbool Concat (SString ss, SString s1, SString s2) {if (s1 [0] + s2 [0] <= MAX_STR_LEN) {ss [0] = s1 [0] + s2 [0]; for (int I = 1; I <= s1 [0]; ++ I) {ss [I] = s1 [I] ;}for (int j = 1; j <= s2 [0]; ++ j) {ss [s1 [0] + j] = s2 [j] ;}} after else // is exceeded, if s1 is used to intercept s2 or s2, {ss [0] = MAX_STR_LEN; for (int I = 1; I <= MAX_STR_LEN; ++ I) {ss [I] = s1 [I]; if (s1 [0] <MAX_STR_LEN) {ss [s1 [0] + 1] = s2 [I] ;}} ss [s1 [0] + s2 [0] + 1] = '\ 0'; // ensure that a string' \ 0' is identified as return true;} // 4. returns the first address of the new string, char * my_strcat (char * d, const char * str); // 7. returns the SubString of the string ss whose length is len starting from the pos position and saves bool SubString (SString sub, SString ss, size_t pos, size_t len) with array sub) {if (pos <1 | pos> ss [0] | len <1 | len> ss [0]-pos + 1) // when the pos is 0, the original string is directly output. // The Relationship Between the length and position should be considered by adding one {return false;} sub [0] = len; for (int I = 0; I <len; ++ I) {sub [I + 1] = ss [pos + I] ;}return true ;}// 8. returns the position where the same substring appears from the pos position of the ss string and the c string (only the same substring that appears for the first time is returned) size_t Index (SString ss, SString c, size_t pos) {assert (pos <= ss [0] & c [0] <= ss [0]); int I = pos; int j = 1; while (I <= ss [0] & j <= c [0]) {if (ss [I] = c [j]) {I ++; j ++;} else {I = I-j + 2; // compare j = 1 from the next character of the string ss; // compare string c from the beginning} if (j> c [0]) // The comparison is complete {return I-c [0]; // return the position where the first element of the c string appears in the ss} return 0; // not found} // 9. replace bool Replace (SString ss, SString c, SString v) {return true;} // 10. insert the string cbool StrInsert (SString ss, size_t pos, SString c) {for (int I = ss [0] + 1; i> = pos; -- I) // when moving backwards, move '\ 0' together. {int a = c [0]; if (ss [0] + c [0]> MAX_STR_LEN) {a = MAX_STR_LEN-ss [0];} ss [I + a] = ss [I];} int j = 0, r = ss [0]; while (j <= c [0]) {if (ss [0] + c [0]> MAX_STR_LEN) {if (r <MAX_STR_LEN) // insert {r ++;} else {break ;}} ss [pos + j] = c [j + 1] In the c string; // insert all c strings into j ++;} if (ss [0] + c [0] <= MAX_STR_LEN) // determine the length of the inserted string {ss [0] + = c [0];} else {ss [0] = MAX_STR_LEN;} return true;} // 11. delete the string bool StrDelete (SString ss, size_t pos, size_t len) whose start length is len in the ss string) {if (pos <1 | pos> ss [0]-len + 1 | len <1) return false; for (int I = pos + len; I <= ss [0]; I ++) ss [I-len] = ss [I]; ss [0]-= len; return true;} // 12. destroy the deletion string ssbool DestoryString (SString ss) {ss [0] = 0; return true;} // 13. display string void StrPrint (SString ss) {for (int I = 1; I <= ss [0]; ++ I) {cout <ss [I] <"" ;}cout <"\ n ";}

/*************************************** * ******************************** Copyright (c) 2015, WK Studios ** Filename: main. cpp ** Compiler: GCC vc 6.0 ** Author: WK ** Time: 2015 25 5 ************************************* ********************************* // test the function # include "str. h "int main () {SString s1; char a1 [] =" 123456 "; StrAssign (s1, a1); cout <" s1 is: "; StrPrint (s1 ); cout <"\ n"; SString s2; char a2 [] = "123456789900009900"; StrAssign (s2, a2); cout <"s2 is:"; StrPrint (s2); cout <"\ n "; cout <"After Insert:"; StrInsert (s1, 2, s2); StrPrint (s1); cout <"\ n"; cout <"After Delete :"; strDelete (s1, 3, 4); StrPrint (s1); cout <"\ n";/* cout <Index (s1, s2, 4) <"\ n "; // substring function test SString sub; SubString (sub, s1, 1, 3); cout <"sub is:"; StrPrint (sub ); cout <"\ n"; // connection test SString ss; Concat (ss, s1, s2); cout <"After concat Ss is: "; StrPrint (ss); cout <" \ n "; // copy test StrCopy (s1, s2); cout <" After copy s1 is :"; strPrint (s1); cout <"\ n"; // comparison test cout <"result of compare s1 with s2 is:"; if (StrCompare (s1, s2 )! = 0) {if (StrCompare (s1, s2)> 0) cout <"s1 large \ n"; else cout <"s2 large \ n ";} else {cout <"equal \ n";} */return 0 ;}








To be continued: Heap Storage operations of strings"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.