C++ 拆分字符串的程序不是';不行吗?

C++ 拆分字符串的程序不是';不行吗?,c++,C++,我创建了一个程序,根据 例如,假设我输入字符串 Workspace.SiteNet.Character.Humanoid 应该是打印的 Workspace SiteNet Character Humanoid 但是,它打印 Workspace SiteNet.Character Character.Humanoid Humanoid 这是代码 #include "stdafx.h" #include <Windows.h> #include <iostream> #i

我创建了一个程序,根据
例如,假设我输入字符串

Workspace.SiteNet.Character.Humanoid

应该是打印的

Workspace
SiteNet
Character
Humanoid
但是,它打印

Workspace
SiteNet.Character
Character.Humanoid
Humanoid
这是代码

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <vector>
#include <sstream>
#include <WinInet.h>
#include <fstream>
#include <istream>
#include <iterator>
#include <algorithm>
#include <string>
#include <Psapi.h>
#include <tlhelp32.h>


int main() {
  std::string str;
  std::cin >> str;
  std::size_t pos = 0, tmp;
  std::vector<std::string> values;
  while ((tmp = str.find('.', pos)) != std::string::npos) {
    values.push_back(str.substr(pos, tmp));
    pos = tmp + 1;
  }
  values.push_back(str.substr(pos, tmp));

  for (pos = 0; pos < values.size(); ++pos){
    std::cout << "String part " << pos << " is " << values[pos] << std::endl;
  }
  Sleep(5000);
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(){
std::字符串str;
std::cin>>str;
标准:尺寸\u t位置=0,tmp;
std::向量值;
while((tmp=str.find('.',pos))!=std::string::npos){
值。推回(str.substr(pos,tmp));
pos=tmp+1;
}
值。推回(str.substr(pos,tmp));
对于(pos=0;posstd::cout我认为在
s
中找到最后一个点会更容易,将点后所有内容的子字符串推入向量,然后将
s
所有内容置于最后一个点之前。重复此操作直到没有点,然后将
s
推入向量

以下是如何做到这一点:

#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> split_at_dot(std::string& s) {
  std::vector<std::string> matches;
  size_t pos;
  // while we can find the last dot
  while((pos = s.find_last_of(".")) != -1) {
    // push back everything after the dot
    matches.push_back(s.substr(pos+1));
    // update s to be everything before the dot
    s = s.substr(0, pos);
  }
  // there are no more dots - push back the rest of the string
  matches.push_back(s);
  // return the matches
  return matches;
}

int main() {
  std::string s = "Workspace.SiteNet.Character.Humanoid";
  std::vector<std::string> matches = split_at_dot(s);
  for(auto match: matches) {
    std::cout << match << std::endl;
  }
}

请注意,这将以相反的顺序给出您期望的答案。如果按照该顺序获得答案非常重要,则可以使用
std::stack
或在调用函数后简单地反转
std::vector

您的问题是传递给
str.substr

string::substr
接受两个参数:开始位置和要提取的子字符串的长度

std::vector<std::string> split_string(const string& str){
    std::vector<std::string> values;
    size_t pos(0), tmp;
    while ((tmp = str.find('.',pos)) != std::string::npos){
        values.push_back(str.substr(pos,tmp-pos));
        pos = tmp+1;
    }
    if (pos < str.length())  // avoid adding "" to values if '.' is last character in str
        values.push_back(str.substr(pos));
    return values;
}
std::向量拆分字符串(const字符串和str){
std::向量值;
尺寸位置(0),tmp;
while((tmp=str.find('.',pos))!=std::string::npos){
值。推回(str.substr(pos,tmp pos));
pos=tmp+1;
}
if(pos
调试时您看到了什么?您可以尝试添加
std::cout您可以实际使用它,它可以使用任何分隔符,而不仅仅是换行符。它还需要更少的代码并且更简单。@JDoe的重复文章:它们到底有多“完全不同”?-甚至看起来您复制并粘贴了已接受的(但有问题)“解决方案”就在这一页上,这似乎是一个非常明确的承认,这是一个重复。嘿,查理。:)这是以利亚。
std::vector<std::string> split_string(const string& str){
    std::vector<std::string> values;
    size_t pos(0), tmp;
    while ((tmp = str.find('.',pos)) != std::string::npos){
        values.push_back(str.substr(pos,tmp-pos));
        pos = tmp+1;
    }
    if (pos < str.length())  // avoid adding "" to values if '.' is last character in str
        values.push_back(str.substr(pos));
    return values;
}