1. 程式人生 > 其它 >C++ :手動實現string

C++ :手動實現string

技術標籤:c++string

實現string的基本功能

1.使用 MyString s1(“asdf”)可以建立字串物件.
2.使用 MyString s2(‘c’,5)可以創建出"ccccc"
3.使用 MyString s3(100),可以創建出字串"100"
4.獲取字串長度.
5.輸出字串
6.查詢子串和字元.

Main.cpp

#include"MyString.h"
#include<iostream>
#include<random>
#include<string>
#include
<vector>
using namespace std; int main(){ MyString mystr("abcdefghijklmn"); mystr.toString(); MyString mystr2("10"); mystr2.toString(); const char x = 'a'; MyString mystr3(x,5); mystr3.toString(); //vector<int> res = mystr.findOne('f');
vector<int> res = mystr.findAll("jk1"); for (int i = 0;i<res.size();i++) { cout<<res[i]<<endl; } cout<<""<<endl; MyString mystr2(100); mystr2.toString(); return 0; }

MyString.h

#include<iostream>
#include<vector> using namespace::std; class MyString { private: const char *str; int length = 0; public: MyString(const char pstr[]); MyString(const char x,int length); MyString(int x); ~MyString(); void toString(); int my_strlen(const char *str); int getLength(); vector<int> findOne( char x); vector<int> findAll(const char x[]); };

MyString.cpp

#include<iostream>
#include"MyString.h"
#include<random>
#include<string>
#include"vector"
#include<stdio.h>
#include<stdlib.h>
using namespace std;
MyString::MyString(const char pstr[]){
   while(pstr[length]!='\0'){
      length++;
   }
   char *help= new char[length];
   for (int i = 0; i < length; i++){
      help[i] = pstr[i];
   }
   str = help;
}

MyString::MyString(int x){
   vector<char> re;
   while(x>9){
      int res = x%10;
      char ress = res+'0';
      re.push_back(ress);
      x = x/10;
   }
    re.push_back(x+'0');
   length = re.size();
   cout<<length<<endl;
   char *w = new char[length];
   int index = length-1;
   int second_index = 0;
   while(index>=0){
      w[second_index] = re[index];
      index--;
      second_index++;
   }
   str = w;
}

MyString::MyString(const char x,int length){
   char *help= new char[length];
   for (int i = 0; i < length; i++){
      help[i] = x;
   }
   str = help;
   this->length = length;
} 

MyString::~MyString(){

}

void MyString::toString(){
   cout<<""<<endl;
   for (int i = 0; i < length; i++){
      cout<< *(str+i);
   }
   cout<<""<<endl;
}

int MyString::getLength(){
   return length;
}

vector<int> MyString::findOne( char x){
   vector<int> result;
   for (int i = 0; i < length; i++){
      if(*(str+i) == x){
         result.push_back(i);
      }
   }
   cout<<""<<endl;
   return result;
}

vector<int> MyString::findAll(const char x[]){
   vector<int> res;
   int len = 0;
   while(x[len]!='\0'){
      len++;
   }
   int end = length - len;
   for (int i = 0; i <= end; i++){
      bool flags = true;
      int j = i;
      int index = 0;
      while(index<len&&j<i+len){
         if(str[j]!= x[index]){
            flags = false;
         }
         index++;
         j++;
      }
      if(flags){
         res.push_back(i);
      }
   }
   if(res.empty()){
      cout<<"未找到該字串"<<endl;
   }
   return res;
}

Ubuntu 環境下編譯指令碼

#!/bin/bash
g++ -c Main.cpp
g++ -c MyString.cpp
g++ Main.o MyString.o -o result
./result
rm Main.o MyString.o result

將指令碼和三個檔案放在同目錄下,
./指令碼名, 執行後 ./result 可以看到執行結果.