如何从当前迭代器位置获取字符串?

问题描述

我有一个字符串,我想在字符串中进行迭代,然后我想从迭代器pos中获取字符串调用stoi,我不知道我是否足够清楚,代码如下:>

# include <iostream>
# include <string>
# include <cctype>
# include <stack>

int main(int argc,char ** argv)
{
    if (argc != 2) { std::cerr << "Usage: ./a.out <expression>" << std::endl; return (-1); }

    std::stack<int> numbers;
    std::string expr(argv[1]);
    for (auto it = expr.begin(); it != expr.end(); it++)
        if (std::isdigit(*it))
        {
            numbers.push(std::stoi(/*string from it*/));
            while (std::isdigit(*it)) it++;
        }
    while (!numbers.empty())
        std::cout << numbers.top() << std::endl,numbers.pop();
    return (0);
}

我知道我可以只使用索引,但我想以迭代器 C++ 友好的方式来做。谢谢你的帮助 例如:./a.out "hello543 29786++23839 kek22" 应该输出

543
29786
23839
22

解决方法

您需要元素的地址。你这样做:

std::stoi(&*it);

在那一点上,您不需要从 std::string 构造 char* 然后使用迭代器,然后将迭代器转换回 char*。您可以只使用 char*,它可能会更干净:

#include <cctype>
#include <cstdlib>
#include <iostream>
#include <stack>

int main(int argc,char** argv) {
  if (argc != 2) {
    std::cerr << "Usage: ./a.out <expression>\n";
    return -1;
  }

  std::stack<int> numbers;
  char const* str = argv[1];
  while (*str) {
    if (std::isdigit(*str)) {
      char* end;
      numbers.push(std::strtol(str,&end,10)); // note strol
      str = end;
    } else
      ++str;
  }

  while (!numbers.empty()) {
    std::cout << numbers.top() << '\n';
    numbers.pop();
  }
  return 0;
}
,

这是我的提议。 循环中命令行参数的解析是使用字符串迭代器访问元素。 堆栈的反向显示由函数 printStack(std::stack<int>)

完成
# include <iostream>
# include <string>
# include <cctype>
# include <stack>
# include <vector>
# include <sstream>
# include <iterator>

void PrintStack(std::stack<int> s);

int main(int argc,char** argv)
{
    if (argc < 2)
    {
        std::cerr << "Usage: ./a.out <expression>" << std::endl;
        return (-1);
    }

    std::stack<int> numbers;
    //std::string expr(argv[1]);
    std::vector<std::string> expr(argv+1,argv + argc);

    for (auto it = expr.begin(); it != expr.end(); it++)
    {
        //cout <<"****" << *it;
        std::string the_num="";
        // extract the number part of each elements
        for (auto iter =it->begin(); iter != it->end(); iter++)
        {
            // now we have characters
            if (std::isdigit(*iter))
            {
                //cout << "$" << *iter<<endl;
                std::string s(1,*iter);
                the_num += *iter;
            }
            else
            {
                if (the_num != "")
                {
                    numbers.push(stoi(the_num));
                    the_num="";
                }
                else
                {
                    continue;
                }

            }
        }
        if (the_num != "")
        {
            numbers.push(stoi(the_num));
            the_num="";
        }
        the_num="";
    }

    PrintStack(numbers);


    return (0);
}

void PrintStack(std::stack<int> s)
{
    // If stack is empty then return
    if (s.empty())
        return;

    int x = s.top();
    // Pop the top element of the stack
    s.pop();
    // Recursively call the function PrintStack
    PrintStack(s);
    // Print the stack element starting
    // from the bottom
    std::cout << x << std::endl;
    // Push the same element onto the stack
    // to preserve the order
    s.push(x);
}

使用您的示例执行,结果如下:

./find_str hello543 29786++23839 kek22
543
29786
23839
22