字符串数组作为类成员? [英] Array of strings as class member?
本文介绍了字符串数组作为类成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将std:string项从单个字符串转换为字符串数组,我该如何定义它?
我尝试过:
#ifndef ORDER_H
< span class =code-preprocessor> #define ORDER_H
#include < 字符串 >
class 订单
{
私人:
int OrderNum;
std :: string items;
std :: string orderDate;
int customerId;
std :: string paymentMethod;
bool 已批准;
public :
order();
order( int orNum,std :: string itms,std :: string orDate, int cuId,std :: string pay, bool 批准);
~order();
};
#endif // ORDER_H
解决方案
如果你想要一个固定大小的数组 - 将成员声明为一个数组。
std :: string items [ 100 跨度>];
项目[ 0 ] = 零跨度>;
项目[ 1 ] = one 跨度>;
项目[ 2 ] = two ;
如果长度必须是可变的,请使用矢量。
vector - C ++ Reference [ ^ ]
#include < vector >
std :: vector< std :: string>项目;
items.push_back( 0);
items.push_back( one);
items.push_back( two);
how can i make std:string items from single string to array of strings , and how can i define it ?
What I have tried:
#ifndef ORDER_H
#define ORDER_H
#include <string>
class order
{
private:
int OrderNum;
std::string items;
std::string orderDate;
int customerId;
std::string paymentMethod;
bool approved;
public:
order();
order(int orNum,std::string itms, std::string orDate, int cuId, std::string pay, bool approve);
~order();
};
#endif // ORDER_H
解决方案
If you want a fixed size array - declare the member as an array.
std::string items[100]; items[0] = "zero"; items[1] = "one"; items[2] = "two";
If the length must be variable, use a vector.
vector - C++ Reference[^]
#include <vector> std::vector<std::string> items; items.push_back("zero"); items.push_back("one"); items.push_back("two");
这篇关于字符串数组作为类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文