#include <iostream>
#include <vector>
#include <string>
class Wc {
private:
std::string word = 0;
public:
std::string getData() const { return word; }
Wc(std::string WORD) : word(WORD) {}
};
int main() {
std::string symb;
std::vector<Wc>elims;
symb = "name";
elims.emplace_back(symb);
std::cout << elims[0].getData();
}
如何仅输出第一个字符而不是整个字符串?
答案 0 :(得分:0)
#include <iostream>
#include<fstream>
#include<vector>
#include<stdlib.h>
using std::cout;
using std::vector;
class Wc {
private:
std::string word = 0;
public:
std::string getData() const { return word; }
Wc(std::string WORD) : word(WORD){}
};
int main() {
std::string symb;
std::vector<Wc>elims;
symb = "name";
elims.emplace_back(symb);
std::cout << elims[0].getData().c_str()[0];// or use...
std::cout << elims[0].getData()[0];
}
C字符串替代
如果需要,可以使用索引器访问每个字符,或者使用指向向量(elims)的指针来迭代每个字符。 std::strings
是C字符串(又称字符数组)的抽象,请使用c_str()
将任何字符串转换为字符数组。如果需要优化,C字符串可以更快地进行操作,请阅读更多here。希望这会有所帮助!
索引字符串的字符
使用另一个索引来访问各个字符,std::strings