STL和数据从字符串到向量
STL and data from string to Vector
好的,我已经试过了。我有下面的类,我有一个驱动程序,它将读取一个文件,并使用getline获取所有内容,并将其复制为字符串。
在我的驱动程序中,我也有vector<Seminar>
。
我感到困惑的是如何把我的数据从字符串到向量。现在我在想,也许首先我需要创建一个构造函数等等,让它工作?
我似乎不能正确地实现它。
class Seminar
{
public:
Seminar(int number = 0, string date = "yyyy-mm-dd" , string title = "")
{
Number = number;
Date = date;
Title = title;
}
int get_number() const {return Number; }
string get_date() const {return Date; }
string get_title() const {return Title; }
private:
int Number; // Seminar number
string Date; // Date of Seminar
string Title; // Title of Seminar
};
enter code here
vector<Seminar> all;
main()
ifstream InFile;
string Letter;
string File;
cout << "Type Letter from the Menu: "<<endl;
cin >> Letter;
if (Letter == "A" || "a")
{
cout << "What is the file you would like to read: "<<endl;
cin >> File;
InFile.open(File.c_str(),ios::in);
if(InFile)
{
string line = "";
while(getline(InFile,line))
{
cout << line << endl;
}
InFile.close();
}
}`enter code here`
下面的内容应该指向正确的方向:
#include<vector>
#include<iostream>
#include<string>
int main()
{
std::vector<std::string> myStringVector;
myStringVector.push_back("First");
myStringVector.push_back("Second");
std::cout<<myStringVector[0]<<"n"<<myStringVector[1]<<"n";
return 0;
}
我想你可能需要这样做:
Seminar seminar1(<data here>);
std::vector<Seminar> seminarVector;
seminarVector.push_back(seminar1);
如果您有一个vector
std::vector<std::string> foo;
foo.push_back( "hi there!" );
相关文章:
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 构造<int>具有 2 个字符串文字的向量
- 如何将元素添加到向量的字符串位置
- 将向量解析<string>为字符串
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- 如何使用向量字符串令牌构建表达树
- 将 argv 复制到新的全局向量字符串
- 如何读取控制台输入并将其存储到向量字符串中
- 向量字符串push_back在C 中不起作用
- C char数组和向量字符串
- 可以直接初始化向量<字符串 *> 而不使用 new 吗?
- 迭代向量字符串的字符(C++禁止指针和整数之间的比较)
- 比较两个向量字符串的相似性C++
- 我有麻烦将字符串放入向量字符串
- 如何从一个向量字符串得到一个字符串的联合集
- 函数返回向量/字符串,但不返回数组
- 向量向量字符串固定大小
- c++向量字符串排序不工作
- C++将字符串数组[]复制到向量<字符串>