串联数组元素c++的子串
substr concatenating array elements c++
我正在构建一个函数,从数组中提取姓氏并将名称打印到屏幕上。我已经能够提取数组中的第一个姓氏,但输出将第一个索引的名字与以下索引位置的姓氏连接起来。下面是我的代码,谢谢你的帮助!
{
stringstream ss;
string name;
for(int i=0; i < elements; i++)
{
ss << first[i];
ss >> name;
int pos = name.find(",");
cout << pos;
string last = name.substr(0,pos);
cout << """ << last << """ << endl;
}
cout << endl;
}
这是因为您使用字符串流的方式。
每次从数组中读取新名称时,都会将其添加到输出缓冲区中。然后从缓冲区中读取下一个单词,因此,您永远不会跳过第一个名字。对于你正在做的事情,你可以把字符串流全部消除,只使用标准的输入和输出(cin/cout)。否则,每次读取新字符串时都要清除缓冲区(ss.clear());
for(int i = 0; i < elements.size(); i++){
int pos = elements[i].find(",");
cout << """ << elements[i].substr(0,pos) << """ << endl;
}
尝试以下代码:
int main()
{
string name = "asd,kkaa";
int pos = name.find(",");
if(string::npos != pos)
{
string part1 = name.substr(0,pos);
string part2 = name.substr(pos+1);
cout << """ << part1 << """ << endl;
cout << """ << part2 << """ << endl;
}
return 0;
}
// output:
// "asd"
// "kkaa"
我觉得可以,所以可以给我们更多你的输入值吗?
相关文章:
- 空基优化子对象的地址
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 继承期间显示未知行为的子类
- 将结构向量排序为子组
- 通过指向指针数组的指针访问子类的属性
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 从父类方法返回子类对象
- 如何在C++中高效地构造随机骰子
- 如何使用tinyxml2从XML加载父实体和子实体
- 求最长递增子序列C++的长度
- 如何从子成员函数修改父公共成员变量
- c++, 在子类中,如何在没有对象的情况下访问父类的方法?
- 最长的回文子串(C++帮助)
- 如何在 C++11 中的类这样的位集中有效地支持"子位串"?
- 串联数组元素c++的子串
- 对于给定的n个数字序列,找到具有最大总和和尽可能低的计算复杂度的子串
- 将密文分成子串(vigenere密码)
- C中相同字符子串的递归算法
- 查找所有最长公共子串