Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 试图从函数“0”向类列表追加值;“分裂分子”;在C++;_C++_Arrays_List_Function_Class - Fatal编程技术网

C++ 试图从函数“0”向类列表追加值;“分裂分子”;在C++;

C++ 试图从函数“0”向类列表追加值;“分裂分子”;在C++;,c++,arrays,list,function,class,C++,Arrays,List,Function,Class,因此,我试图编写一个名为“splitoList”的函数来获取3个参数,一个字符串,另一个定义拆分第一个字符串的位置的字符串,以及一个要将数据附加到其中的类列表。这是我到目前为止所拥有的 split.h #include <iostream> #include <sstream> using namespace std; string splitoList(string s, string spechar, List <string> lst) { siz

因此,我试图编写一个名为“splitoList”的函数来获取3个参数,一个字符串,另一个定义拆分第一个字符串的位置的字符串,以及一个要将数据附加到其中的类列表。这是我到目前为止所拥有的

split.h

#include <iostream>
#include <sstream>
using namespace std;

string splitoList(string s, string spechar, List <string> lst) {
  size_t pos = 0;
  string token;
  int i = 0;
  if (spechar == "") {
    for (char i : s) {
      string n(1,i);
      lst.append(n);
    }
    return s;
  } else {
    while ((pos = s.find(spechar)) != std::string::npos) {
      token = s.substr(0, pos);
      lst.append(token);
      s.erase(0, pos + spechar.length());
    }
    //cout << s;
    lst.append(s);
    lst.print();
    return s;
  }
}
#包括
#包括
使用名称空间std;
字符串拆分列表(字符串s、字符串spechar、列表lst){
大小\u t pos=0;
字符串标记;
int i=0;
如果(spechar==“”){
用于(字符i:s){
串n(1,i);
附加(n);
}
返回s;
}否则{
while((pos=s.find(spechar))!=std::string::npos){
令牌=s.substr(0,位置);
附加(令牌);
s、 擦除(0,pos+spechar.length());
}

//cout您需要使用一个引用参数更改函数的原型,然后print语句按照您的预期工作

字符串拆分列表(字符串s、字符串spechar、列表和lst)

lst
作为值传递时,函数
splitoList
中的实例是主函数中实例的副本;作为参考,它们是相同的实例

参考常见问题:


您需要使用一个引用参数更改函数的原型,然后print语句按照您的预期工作

字符串拆分列表(字符串s、字符串spechar、列表和lst)

lst
作为值传递时,函数
splitoList
中的实例是主函数中实例的副本;作为参考,它们是相同的实例

参考常见问题:


您需要传入
List&lst
作为函数参数,否则它只是传入
main()
的列表的副本,不修改原始列表。您需要传入
List&lst
作为函数参数,否则它只是传入
main()
的列表的副本,不修改原始列表。
#include <iostream>
#include <cstdarg>
using namespace std;

template <class T>
class List {
  public:
  int size;
  T* list;
  List() {
    size = 0;
    list = new T[100];
  }

  void append(T data) {
    list[size] = data;
    size++;
  }

  int select(int pos) {
    return list[pos];
  }

  void clear() {
   for (int i = 0; i <= size; i++) {
     list[i] = list[i - 1];
   }
   size = 0;
  }

  void sort(bool reverse) {
    int temp;
    if (reverse == true) {
      for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
          if (list[i] > list[j]) {
            temp = list[i];
            list[i] = list[j];
            list[j] = temp;
          }
        }
      }
    } 
    else {
      for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
          if (list[i] < list[j]) {
            temp = list[i];
            list[i] = list[j];
            list[j] = temp;
          }
        }
      }
    }
  }
  
  void remove() {
    if (size == 0) {
      cout << "[ ]\n";
      return;
    }
    else {
      for (int i = 0; i < size - 1; i++) {
        list[i] = list[i + 1];
      }
      size--;
    }
  }

  void reverse() {
    int temp;
    for (int i = size - 1; i > 0; i--) {
      temp = list[i];
      list[i] = list[size-i-1];
      list[size-i-1] = temp;
    }
  }

  void count(int item) {
    int cnt = 0;
    for (int i = 0; i < size; i++) {
      if (list[i] == item) {
        cnt++;
      }
    }
    cout << cnt << endl;
  }

  void print() {
    if (size == 0) {
      cout << "[ ]\n";
      return;
    }
    cout << "[";
    for (int i = 0; i < size; i++) {
      if (i == size - 1) {
        cout << list[i];
      }
      else {
        cout << list[i] << ", ";
      }
    }
    cout << "]\n";
  }
  
  List operator+(List l2) {
    List l3;
    for (int i = 0; i < this->size; i++) {
      l3.append(this->list[i]);
    }
    for (int i = 0; i < l2.size; i++) {
      l3.append(l2.list[i]);
    }
    return l3;
  }
};
#include <iostream>
#include <sstream>
#include "lists.h"
#include "split.h"
using namespace std;

int main() {
  List <string> l1;
  l1.append("Hello!"); l1.append("World!");
  l1.print();
  l1.clear();
  string n = "Hello World";
  splitoList(n, " ", l1);
  l1.print();
  return 0;
}