我正在尝试迭代字符串的单词.
可以假设该字符串由用空格分隔的单词组成.
请注意,我对C字符串函数或那种字符操作/访问不感兴趣.另外,请在答案中优先考虑优雅而不是效率.
我现在最好的解决方案是:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string s = "Somewhere down the road";
istringstream iss(s);
do
{
string subs;
iss >> subs;
cout << "Substring: " << subs << endl;
} while (iss);
}
Run Code Online (Sandbox Code Playgroud)
有没有更优雅的方式来做到这一点?
我有以下代码:
std::string str = "abc def,ghi";
std::stringstream ss(str);
string token;
while (ss >> token)
{
printf("%s\n", token.c_str());
}
Run Code Online (Sandbox Code Playgroud)
输出是:
abc
def,ghi
因此,stringstream::>>运算符可以按空格分隔字符串,但不能用逗号分隔.反正有没有修改上面的代码,以便我可以得到以下结果?
输入:"abc,def,ghi"
输出:
abc
def
ghi
我试图插入一个由空格分隔的字符串到一个字符串数组,而不使用C++中的vector.例如:
using namespace std;
int main() {
string line = "test one two three.";
string arr[4];
//codes here to put each word in string line into string array arr
for(int i = 0; i < 4; i++) {
cout << arr[i] << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望输出为:
test
one
two
three.
Run Code Online (Sandbox Code Playgroud)
我知道在C++中已经有很多问题要求字符串>数组.我意识到这可能是一个重复的问题,但我找不到任何满足我条件的答案(将字符串拆分为数组而不使用向量).如果这是一个重复的问题,我会提前道歉.
对不起,我的C/C++不是那么好,但是对我来说,下面的现有代码看起来像垃圾.它也有一个错误 - 当str ="07/02/2010"由'\ 0'终止时失败 - .我认为不是修复错误,而是可以重写.在Python中它只是'kas\nhjkfh kjsdjkasf'.split().我知道这是C-ish代码,但分割字符串不是那么复杂!坚持使用相同的签名,而不使用额外的库,我怎样才能改进它 - 让它简短又甜蜜?我可以说这个代码闻起来,例如因为else句子一直到最后.
线路失败:
_tcsncpy_s(
s.GetBuffer((int) (nIndex-nLast)),
nIndex-nLast,
psz+nLast,
(size_t) (nIndex-nLast)
);
Run Code Online (Sandbox Code Playgroud)
当字符串"07/02/2010"以'\ 0'结尾时,它将尝试将11个字符写入只有10个字符长的缓冲区.
全功能:
#define
// This will return the text string as a string array
// This function is called from SetControlText to parse the
// text string into an array of CStrings that the control
// Gadgets will attempt to interpret
BOOL CLVGridDateTimeCtrl::ParseTextWithCurrentFormat(const CString& str, const CGXStyle* pOldStyle, CStringArray& strArray )
{
// Unused:
pOldStyle;
// we assume …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,提示用户:
第一部分很好,我创建了一个动态分配的数组(必需)并使其成为用户想要的大小。
我被困在下一部分。用户应输入一系列由逗号分隔的整数,例如:1,2,3,4,5
我如何接收这些整数并将它们放入我动态分配的数组中?我读到默认情况下 cin 接受以空格分隔的整数,我可以将其更改为逗号吗?
请以最简单的方式解释,我是编程初学者(对不起!)
编辑: TY 这么多答案。问题是我们还没有涵盖向量......有没有一种方法只使用我拥有的动态分配的数组?
到目前为止,我的函数看起来像这样。我在 main.js 中创建了一个默认数组。我计划将它传递给这个函数,创建新数组,填充它,并更新指针以指向新数组。
int *fill (int *&array, int *limit) {
cout << "What is the desired array size?: ";
while ( !(cin >> *limit) || *limit < 0 ) {
cout << " Invalid entry. Please enter a positive integer: ";
cin.clear();
cin.ignore (1000, 10);
}
int *newarr;
newarr = new int[*limit]
//I'm stuck here
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个用于OpenGL的小型OBJ文件加载器,但是根本没有读取面部.在添加面部阅读器之前,程序读取的一切都很好,所以我在这里有点难过.这是我的代码:
void OBJLoader::LoadObjFile(std::string f,std::vector<float>& v,std::vector<float>& n,std::vector<float>& u)
{
std::ifstream file;
file.open(f);
OutputDebugStringA("OPENING FILE-\n");
OutputDebugStringA(f.c_str());
std::string vertex,normal,uv,face;
std::string data;
std::string data2;
std::string data3 = "";
uv = "vt";
normal = "vn";
vertex = "v";
face = "f";
std::size_t var1;
int i = 1;
int j = 1;
int k = 1;
bool loop = true;
int loopcount = 1;
std::vector<float> tV,tU,tN;
if(file.is_open())
{
while(std::getline(file,data) && file.good())
{
var1 = data.find(vertex);
if(var1 != std::string::npos && var1 < data.size() && data[1] == 0x20) …Run Code Online (Sandbox Code Playgroud) 有点我的代码如下所示:
static int myfunc(const string& stringInput)
{
string word;
stringstream ss;
ss << stringInput;
while(ss >> word)
{
++counters[word];
}
...
}
Run Code Online (Sandbox Code Playgroud)
这里的目的是获取一个输入字符串(由空格''分隔)到字符串变量中word,但这里的代码似乎有很多开销 - 将输入字符串转换为字符串流并从字符串流读取到目标字符串.
是否有更优雅的方式来实现相同的目的?
重复调用以vector::size()重新O(n)计算元素的数量(计算)或将此值存储在某处(O(1)查找).例如,在下面的代码中
// Split given string on whitespace
vector<string> split( const string& s )
{
vector<string> tokens;
string::size_type i, j;
i = 0;
while ( i != s.size() ) {
// ignore leading blanks
while ( isspace(s[i]) && i != s.size() ) {
i++;
}
// found a word, now find its end
j = i;
while ( !isspace(s[j]) && j != s.size() ) {
j++;
}
// if we found a word, add …Run Code Online (Sandbox Code Playgroud)我正在阅读来自诸如"5 8 12 45 8 13 7"之类文件的输入行.
我可以将这些整数直接放入数组中,还是必须先将它们放入字符串中?
如果最初使用字符串是必须的,我该如何将这个整数字符串转换为数组?
输入:"5 8 12 45 8 13 7"=>进入一个数组:{5,8,12,45,8,13,7}