相关疑难解决方法(0)

如何迭代字符串的单词?

我正在尝试迭代字符串的单词.

可以假设该字符串由用空格分隔的单词组成.

请注意,我对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)

有没有更优雅的方式来做到这一点?

c++ string split

2895
推荐指数
43
解决办法
214万
查看次数

错误:从'int'类型的右值开始,无效初始化'int&'类型的非const引用

错误的形式:

int &z = 12;
Run Code Online (Sandbox Code Playgroud)

正确形式:

int y;
int &r = y;
Run Code Online (Sandbox Code Playgroud)

问题:
为什么第一个代码错了?标题中错误的"含义 "是什么?

c++ pointers reference

79
推荐指数
4
解决办法
15万
查看次数

std :: ostringstream打印c-string的地址而不是其内容

我偶然发现了一个我最初无法解释的奇怪行为(见ideone):

#include <iostream>
#include <sstream>
#include <string>

int main() {
  std::cout << "Reference     : "
            << (void const*)"some data"
            << "\n";

  std::ostringstream s;
  s << "some data";
  std::cout << "Regular Syntax: " << s.str() << "\n";

  std::ostringstream s2;
  std::cout << "Semi inline   : "
            << static_cast<std::ostringstream&>(s2 << "some data").str()
            << "\n";

  std::cout << "Inline        : "
            << dynamic_cast<std::ostringstream&>(
                 std::ostringstream() << "some data"
               ).str()
            << "\n";
}
Run Code Online (Sandbox Code Playgroud)

给出输出:

Reference     : 0x804a03d
Regular Syntax: some data
Semi inline   : some …
Run Code Online (Sandbox Code Playgroud)

c++ stream

19
推荐指数
2
解决办法
6599
查看次数

交换临时参考元组

我正在编写一个自定义迭代器,当dereferenced返回一个引用元组时.由于元组本身是短暂的,我不认为我可以从operator*()返回引用.我认为我的迭代器在语义上是有意义的,因为它具有引用语义,即使operator*返回一个值.

问题是,当我尝试调用std :: swap(或者更确切地说,当std :: sort执行时),如下所示,我得到错误,因为交换需要l值.这个问题有一个简单的解决方法吗?

#include <vector>

class test {
  public:
  test()
    :v1(10), v2(10)
  {}

  class iterator {
    public:
    iterator(std::vector<int>& _v1,
             std::vector<int>& _v2)
      :v1(_v1), v2(_v2){}

    std::tuple<int&, int&> operator*(){
      return std::tuple<int&, int&>{v1[5], v2[5]};
    }
    std::vector<int>& v1;
    std::vector<int>& v2;
  };

  std::vector<int> v1, v2;
};



int main(){
  test t;
  //error, Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:3003:1: note: candidate function [with _Tp = std::__1::tuple<int &, int &>] not viable: expects an l-value for 1st argument

  //deep within the bowels of std::sort ...
  std::swap(*(test::iterator(t.v1, t.v2)),
            *(test::iterator(t.v1, t.v2)));
}
Run Code Online (Sandbox Code Playgroud)

c++ swap tuples rvalue

6
推荐指数
1
解决办法
567
查看次数

什么是标准的魔力:移动

以下代码使VC2010失败:

//code1
std::string& test1(std::string&& x){
  return x;
}
std::string str("xxx");
test1(str);  //#1 You cannot bind an lvalue to an rvalue reference

//code2 
std::string&& test1(std::string&& x){
  return x;  //#2 You cannot bind an lvalue to an rvalue reference
}
Run Code Online (Sandbox Code Playgroud)

有一些文章可以解释#1,但我不明白为什么#2也会失败.

让我们看看std :: move是如何实现的

template<class _Ty> inline
    typename tr1::_Remove_reference<_Ty>::_Type&&
        move(_Ty&& _Arg)
    {   // forward _Arg as movable
    return ((typename tr1::_Remove_reference<_Ty>::_Type&&)_Arg);
    }
Run Code Online (Sandbox Code Playgroud)
  1. move的参数仍然是右值引用,但move(str)是可以的!
  2. move也返回rvalue.

什么是std的魔力:移动?

谢谢

c++ rvalue-reference c++11

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

在C++中读取单个输入行中的整数列表

我正在尝试从单个输入行读取多个整数到例如.输入:100 200 300 400,所以数组是:a [0] = 100,a [1] = 200,a [2] = 300,a [3] = 400事实是,整数的数量是未知的,所以数组的大小是未知的.

c++ integer

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

C++在.txt文件中查找整数值

我有一个map.txt文件:

[Map]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 …
Run Code Online (Sandbox Code Playgroud)

c++ integer position file edit

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

将std :: unique_ptr与std :: istream一起使用?

我正在用c ++编写一个程序,它可以从std :: cin或std :: istringstream获取文件的输入(作为命令行arg传递给它).它工作正常,并使用std :: istream*进行管理.我被告知这很糟糕,处理原始指针,所以我决定将它包装在std :: unique_ptr(即std :: unique_ptr)中.问题是它无法编译.至于我可以从错误中辨别出来,std :: istream已经保护自己不被用来分配内存.我试过谷歌搜索它,但我认为之前没有人发布过这样的问题(因为我只看到只引用std :: unique_ptr的问题).有谁知道如何实现这一目标?

edit: errors: In file included from /usr/include/c++/4.8/iostream:40:0,
                 from /home/dtscode/Desktop/SLang/src/main.cpp:1:
/usr/include/c++/4.8/istream: In function ‘int main(int, char**)’:
/usr/include/c++/4.8/istream:606:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_istream()
       ^
compilation terminated due to -Wfatal-errors.
make[2]: *** [CMakeFiles/slang.dir/src/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/slang.dir/all] Error 2
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)

响应这一行:std :: unique_ptr Stream(new std :: istream());

我也尝试过它而不调用istreams构造函数,并且在unique_ptrs构造函数中没有任何东西

编辑2:

#include <iostream>
#include  <fstream>
#include …
Run Code Online (Sandbox Code Playgroud)

c++ stl istream unique-ptr c++11

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