相关疑难解决方法(0)

C++:将字符串和内容分割成std :: vector的优雅方式

我想沿着空格分割一个字符串,我知道标记代表有效的整数.我想将标记转换为整数并用它们填充向量.

我可以使用boost :: split,创建一个令牌字符串的向量,然后使用std :: transform.

你的解决方案是什么?使用boost是可以接受的.

c++ string split

10
推荐指数
1
解决办法
2435
查看次数

从String中提取X和Y坐标

我从CCString中提取b2vec2坐标时遇到问题,这些坐标来自cocos2dx和box2d.

我尝试过使用strtk,但我无法使用它

任何帮助都会很棒.

谢谢

字符串的布局是"x,yx,yx,y"我想将x和y放入b2vec2的数组中

string extract box2d cocos2d-x

10
推荐指数
1
解决办法
255
查看次数

如何通过分隔符将字符串拆分为数组?

我是编程新手.我一直在尝试用C++编写一个函数,它将字符串的内容分解为给定参数的字符串数组,例如:

string str = "___this_ is__ th_e str__ing we__ will use__";
Run Code Online (Sandbox Code Playgroud)

应该返回字符串数组:

cout << stringArray[0]; // 'this'
cout << stringArray[1]; // ' is'
cout << stringArray[2]; // ' th'
cout << stringArray[3]; // 'e str'
cout << stringArray[4]; // 'ing we'
cout << stringArray[5]; // ' will use'
Run Code Online (Sandbox Code Playgroud)

我可以很好地对字符串进行标记,但对我来说最困难的部分是如何在为其指定当前字符串toke之前指定stringArray中的元素数量以及如何从函数返回stringArray.

有人会告诉我如何写这个功能吗?

Edit1: 我不一定需要结果在字符串数组中只是任何容器,我可以调用它作为常规变量与某种索引.

c++ string function

9
推荐指数
2
解决办法
5万
查看次数

转换str字符串以在std :: getline分隔符中使用

我试图std::string取值并在std::getline()功能中使用它作为分隔符,但无论我做什么编译器给我错误.这就是我想要做的:

std::stringstream ss(s);
    std::string item;
    std::string delim ="&&=";
    int ssize = delim.size();
    int newssize = ssize+1;
    char del[SSIZE];  // also here when i try gives error
    strcpy(del,delim.c_str());

        char * delrr[1024] = delim.c_str();  //gives error 

        while(std::getline(ss,item,delrr)) {
           elems.push_back(item);
    }
Run Code Online (Sandbox Code Playgroud)

我得到的错误总是关于const char*convertion.

 error C2440: 'initializing' : cannot convert from 'const char *' to 'char *[1024]'
1>        There are no conversions to array types, although there are conversions to references or pointers to arrays
1>.\UT.cpp(179) : error C2780: 'std::basic_istream<_Elem,_Traits> …
Run Code Online (Sandbox Code Playgroud)

c++ string split

9
推荐指数
1
解决办法
2260
查看次数

如何拆分文件行与空格和制表区别?

我有一个以下格式的文件

mon 01/01/1000(TAB)你好(TAB)你好吗?

有没有办法以这种方式阅读文本'\t'单独作为分隔符(而不是空格)?

所以样本输出可以是,

星期一01/01/1000

你好

你好吗

我无法使用fscanf(),因为它只读到第一个空格.

c++ string whitespace tabs split

8
推荐指数
2
解决办法
1万
查看次数

c ++ tokenize std string

可能重复:
如何在C++中对字符串进行标记?

您好我想知道如何用strtok标记std字符串

string line = "hello, world, bye";    
char * pch = strtok(line.c_str(),",");
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

error: invalid conversion from ‘const char*’ to ‘char*’
error: initializing argument 1 of ‘char* strtok(char*, const char*)’
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种快速简便的方法,因为我认为它不需要太多时间

c++ tokenize strtok

6
推荐指数
2
解决办法
3万
查看次数

在c ++中以不同方式处理多个输入命令的正确方法是什么?

我有一个程序从用户接收命令,它将以不同的方式处理不同的命令.例如:

ADD_STUDENT ALEX 5.11 175
ADD_TEACHER MERY 5.4  120 70000
PRINT MERY 
REMOVE ALEX
PRINT TEACHER SALARY
PRINTALL 
Run Code Online (Sandbox Code Playgroud)

因此,我需要检查每一行,看看输入是由什么组成的.

这是我的代码,但我想我误解了"工作方式".有人可以给我一个建议吗?并告诉我为什么我的代码不能像我预期的那样工作?

string line;
while(getline(cin, line))
{
  //some initialization of string, float variable
  std::istringstream iss(line);
  if(iss >> command >> name >> height >> weight)
   ..examine the command is correct(ADD_STUDENT) and then do something..
  else if(iss >> command >> name >> height >> weight >> salary)
   ..examine the command is correct(ADD_TEACHER) and then do something...
  else if(iss >> command >> name)
   ..examine the …
Run Code Online (Sandbox Code Playgroud)

c++

6
推荐指数
3
解决办法
474
查看次数

使用空格作为分隔符在C/C++中拆分一行

可能重复:
如何在C++中对字符串进行标记?

伪代码:

    Attributes[] = Split line(' ')
Run Code Online (Sandbox Code Playgroud)

怎么样?

我一直这样做:

  char *pch;
  pch = strtok(line," ");
  while(pch!=NULL)
  {
      fputs ( pch, stdout ); 


  }
Run Code Online (Sandbox Code Playgroud)

并获得一个非书面,卡住的退出文件.这有什么问题吗?好吧,事情甚至没有满足我的伪代码要求,但我对如何将令牌(作为char数组)索引到我的数组感到困惑,我想我应该写一个2-dim数组?

c c++

5
推荐指数
2
解决办法
9146
查看次数

与解析相关的令牌究竟是什么

我必须在 C++ 中使用解析器和编写器,我正在尝试实现这些功能,但是我不明白令牌是什么。我的功能/操作之一是检查是否有更多令牌要生产

bool Parser::hasMoreTokens()

我到底该怎么做,请帮忙

所以!

我正在打开一个带有文本的文本文件,所有单词都是小写的。我如何去检查它是否有更多令牌?

这就是我所拥有的

bool Parser::hasMoreTokens() {

while(source.peek()!=NULL){
    return true;
}
    return false;
}
Run Code Online (Sandbox Code Playgroud)

c++ stream ifstream ostream

5
推荐指数
2
解决办法
2452
查看次数

在字符串中查找确切的substr

我有一个文本文件,其中包含以下文本

许可证="123456"

GeneralLicense ="56475655"

我想搜索License以及GeneralLicense.

while (getline(FileStream, CurrentReadLine))
{

    if (CurrentReadLine.find("License") != std::string::npos)
    {
        std::cout << "License Line: " << CurrentReadLine;
    }
    if (CurrentReadLine.find("GeneralLicense") != std::string::npos)
    {
        std::cout << "General License Line: " << CurrentReadLine;
    }
}
Run Code Online (Sandbox Code Playgroud)

由于单词License也出现在单词中GeneralLicense所以if-statement在行中if (CurrentReadLine.find("License") != std::string::npos)变为真实两次.

如何指定我要搜索确切的子字符串?

更新:我可以按照一些答案提到的顺序撤销或检查是否License在索引零处.但是没有任何ROBOUST(旗帜或其他东西)我们可以精确地寻找完全匹配(像我们在大多数编辑器中那样的东西,例如MS Word等).

c++

5
推荐指数
1
解决办法
1504
查看次数

标签 统计

c++ ×9

string ×5

split ×3

box2d ×1

c ×1

cocos2d-x ×1

extract ×1

function ×1

ifstream ×1

ostream ×1

stream ×1

strtok ×1

tabs ×1

tokenize ×1

whitespace ×1