这花了我很长时间才弄清楚,我研究了很多不同的例子,但有些看起来很复杂,并且出于 A、B、C 的原因,其中有额外的步骤......这就是我需要的 - 让我们说你有一个字符串,我们将其称为字符串 A
字符串 A =“我
所以分隔符当然是 '<'
如何解析这个字符串以取出分隔符?然后我必须创建这些字符串的对象并将它们添加到指针数组中。
请参阅下面的示例:) 请忽略顶部的所有指令。我讨厌猜测我需要什么和不需要什么,所以我开始在所有文件中包含我需要的每一个文件(除了在其他文件中包含文件)。我花了 6 个多小时研究解析,但一无所获,所以我今晚正式结束了......
我猜我也对指针感到困惑,因为我认为下面的行将是一个指针,用于从学生头文件中的 Student 类获取数据,并让它创建一个指针数组,然后我将使用该指针填充对象下面的“add”方法........但它告诉我-无法使用“string *”类型的右值(又名“basic_string
Student* classRosterArray = new string[5];
#pragma once
#include <stdio.h>
#include <string>
#include <iostream>
#include <print>
#include <iomanip>
#include "student.hpp"
using namespace std;
class Roster
{
private:
// Array of pointers, size = 5-- I thought I was supposed to use a pointer to reference the Student class.... and call the new array which we are adding objects into classRosterArray?
Student* classRosterArray = new string[5];
//string* classRosterArray = new string[5]; --Is this what is needed??
public:
void parse(string row);
void add(string studentId,
string firstName,
string lastName,
string emailAddress,
int age,
int daysToComp3Course,
DegreeProgram degreeProgram);
};
如何解析这个字符串以取出分隔符?
要提取每个单词,请使用 std::getline 和 std::istringstream:
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::string A = "I<Like<Butterscotch<candies<so<much";
std::istringstream strm(A);
std::string word;
while (std::getline(strm, word, '<'))
std::cout << word << "\n";
}
输出:
I
Like
Butterscotch
candies
so
much