如果用空格分隔,我怎么能得到像"Ac milan"和"Real Madryt"这样的字符串?
这是我的尝试:
string linia = "Ac milan ; Real Madryt ; 0 ; 2";
str = new char [linia.size()+1];
strcpy(str, linia.c_str());
sscanf(str, "%s ; %s ; %d ; %d", a, b, &c, &d);
Run Code Online (Sandbox Code Playgroud)
但它不起作用; 我有:a= Ac; b = (null); c=0; d=2;
我有逗号分隔的字符串,我需要从中提取值.问题是这些字符串永远不会是固定大小.所以我决定迭代逗号组并阅读其间的内容.为了做到这一点,我创建了一个函数,它返回样本字符串中每个匹配项的位置.
这是一个聪明的方法吗?这被认为是不好的代码吗?
#include <string>
#include <iostream>
#include <vector>
#include <Windows.h>
using namespace std;
vector<int> findLocation(string sample, char findIt);
int main()
{
string test = "19,,112456.0,a,34656";
char findIt = ',';
vector<int> results = findLocation(test,findIt);
return 0;
}
vector<int> findLocation(string sample, char findIt)
{
vector<int> characterLocations;
for(int i =0; i < sample.size(); i++)
if(sample[i] == findIt)
characterLocations.push_back(sample[i]);
return characterLocations;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用此线程中找到的方法拆分字符串,但我正在尝试将其调整为wstring.但是,我偶然发现了一个奇怪的错误.检查代码:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main(void)
{
wstring str(L"Börk börk");
wistringstream iss(str);
vector<wstring> tokens;
copy(istream_iterator<wstring>(iss), // I GET THE ERROR HERE
istream_iterator<wstring>(),
back_inserter< vector<wstring> >(tokens));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
确切的错误消息是:
error: no matching function for call to 'std::istream_iterator<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, char, std::char_traits<char>, int>::istream_iterator(std::wistringstream&)'
Run Code Online (Sandbox Code Playgroud)
我认为它说它不能使用传递的实例化istream_iterator iss(这是一个w istringstream而不是istringstream).这是在使用XCode和GCC 4.2的Mac上.和AFAIK没有wistring_iterator或类似的东西.
当我使用非wstring版本时它完美地工作.正如您可能看到我已经更改了要使用的声明wstring,wistringstreams和vector<wstring>,只是将所有内容替换为使用宽版本.
什么可能导致这个,为什么它不接受wstring-version?
所以我们有一个简单的分裂:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
vector<string> split(const string& s, const string& delim, const bool keep_empty = true) {
vector<string> result;
if (delim.empty()) {
result.push_back(s);
return result;
}
string::const_iterator substart = s.begin(), subend;
while (true) {
subend = search(substart, s.end(), delim.begin(), delim.end());
string temp(substart, subend);
if (keep_empty || !temp.empty()) {
result.push_back(temp);
}
if (subend == s.end()) {
break;
}
substart = subend + delim.size();
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
我最近看到以下代码块作为对这个问题的回答:在C++中拆分一个字符串?
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string>
&elems) {
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
Run Code Online (Sandbox Code Playgroud)
为什么返回传递的引用数组"elems"在这里如此重要?我们不能使这个为void函数,或者返回一个整数来表示成功/失败吗?我们正在编辑实际的阵列,对吧?
谢谢!
可能重复:
在C++中拆分字符串
我试图创建一个模仿函数行为的getline()函数,可以选择使用分隔符将字符串拆分为标记.该函数接受2个字符串(第二个是通过引用传递)和一个char分隔符的类型.它遍历第一个字符串的每个字符,将其复制到第二个字符串,并在到达分隔符时停止循环.true如果第一个字符串在分隔符后面有更多字符,则返回,false否则返回.最后一个字符的位置保存在静态变量中.由于某种原因,程序进入无限循环并且没有执行任何操作:
const int LINE_SIZE = 160;
bool strSplit(string sFirst, string & sLast, char cDelim) {
static int iCount = 0;
for(int i = iCount; i < LINE_SIZE; i++) {
if(sFirst[i] != cDelim)
sLast[i-iCount] = sFirst[i];
else {
iCount = i+1;
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
该功能以下列方式使用:
while(strSplit(sLine, sToken, '|')) {
cout << sToken << endl;
}
Run Code Online (Sandbox Code Playgroud)
为什么它会进入一个无限循环,为什么它不起作用?我应该补充说istringstream,如果可能的话,我对没有使用的解决方案感兴趣.
我想使用字符串拆分功能获得此问题的最高评分答案:
为方便起见,复制答案:
#include <string>
#include <sstream>
#include <vector>
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
Run Code Online (Sandbox Code Playgroud)
我在这里有一个问题.为什么宣言std::vector<std::string> &split(...)不是void split(...)?作为我们收到的论据std::vector<std::string> &elems,这就是我们想要的结果.如果我们收到参考,为什么还要退货?
我有:
int &multA (int &n)
{
n = 5*n;
return n;
}
void multB (int &n)
{
n = 5*n;
}
Run Code Online (Sandbox Code Playgroud)
和:
int …Run Code Online (Sandbox Code Playgroud) 我试图制作一个C ++程序来接收用户输入,并提取字符串中的各个单词,例如,“ Hello to Bob”将得到“ Hello”,“ to”,“ Bob”。最终,我将这些推入字符串向量中。这是我在设计代码时尝试使用的格式:
//string libraries and all other appropriate libraries have been included above here
string UserInput;
getline(cin,UserInput)
vector<string> words;
string temp=UserInput;
string pushBackVar;//this will eventually be used to pushback words into a vector
for (int i=0;i<UserInput.length();i++)
{
if(UserInput[i]==32)
{
pushBackVar=temp.erase(i,UserInput.length()-i);
//something like words.pushback(pushBackVar) will go here;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这仅适用于字符串中遇到的第一个空格。如果单词之前有任何空格(例如,如果我们有“ Hello my World”,则在第一个循环之后,pushBackVar将为“ Hello”)将不起作用。当我想要“ Hello”和“ my”时,在第二个循环后单击“ Hello my”。)如何解决此问题?还有其他更好的方法来从字符串中提取单个单词吗?我希望我不会混淆任何人。
如果我想迭代字符串中的单个单词(由空格分隔),那么显而易见的解决方案是:
std::istringstream s(myString);
std::string word;
while (s >> word)
do things
Run Code Online (Sandbox Code Playgroud)
然而,这是非常低效的.在初始化字符串流时复制整个字符串,然后将每个提取的单词一次一个地复制到word变量中(这接近于第二次复制整个字符串).有没有办法在不手动迭代每个字符的情况下对此进行改进?