小编wee*_*eeo的帖子

C++从1个字符串转换为字符串?

我真的没有找到任何接近的答案......

相反的方式非常简单,如str [0]

但我只需要将1个字符串转换为字符串...

像这样:

char c = 34;
string(1,c);
//this doesn't work, the string is always empty.

string s(c);
//also doesn't work.

boost::lexical_cast<string>((int)c);

//also return null
Run Code Online (Sandbox Code Playgroud)

c++ casting

108
推荐指数
3
解决办法
24万
查看次数

C++如何将数组插入哈希集?

我需要在哈希集中插入一维数组.

但是我在编译时遇到了错误.

#include <stdio.h>
#include <stdlib.h>
#include <hash_set.h>
using namespace std;
int hash_comp(const int* state1,const int* state2) {
    int result = 0;

    for (i = 0; i < 16; i++) 
    {
        if (state1[i] != state2[i]) {
            result = -1;
        }

    }
    return result;
}
struct eqArray
{
    bool operator()(const int* a1,const int* a2) const
  {
    return hash_comp(a1,a2) == 0;
  }
};
hash_set<int*,hash<int*>,eqArray> closelist;
int main(int argc, char** argv)
{
   const int sn[16] = {1,2,3,4,5,6,0,8,9,10,11,12,13,14,7,15};
   closelist.insert(sn);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)
/usr/include/c++/4.2.1/ext/hashtable.h: In …
Run Code Online (Sandbox Code Playgroud)

c++ arrays hashset

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

如何链接zlib库

嗨,我正在使用boost和zlib过滤器来压缩和解压缩数据.在boost页面的指令中,它说如果.cpp文件依赖于外部库,你必须从源代码构建它或链接到预构建的二进制文件.

我使用mac端口安装boost和zlib.我将boost libarary包含为-I/opt/local/include

我的代码:

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>

int main() 
{
    using namespace std;

    ifstream file("hello.z", ios_base::in | ios_base::binary);
    filtering_streambuf<input> in;
    in.push(zlib_decompressor());
    in.push(file);
    boost::iostreams::copy(in, cout);
}
Run Code Online (Sandbox Code Playgroud)

我想知道如何链接预建的zlib外部库?它给了我这个编译问题:

mpic++ -o local ods_v2.0.cpp -I/opt/local/include
Undefined symbols for architecture x86_64:
  "boost::iostreams::zlib_error::check(int)", referenced from:
      long boost::iostreams::symmetric_filter<boost::iostreams::detail::zlib_decompressor_impl<std::allocator<char> >, std::allocator<char> >::write<boost::iostreams::detail::linked_streambuf<char, std::char_traits<char> > >(boost::iostreams::detail::linked_streambuf<char, std::char_traits<char> >&, char const*, long) in ods_v2-DjDcji.o
      void boost::iostreams::symmetric_filter<boost::iostreams::detail::zlib_decompressor_impl<std::allocator<char> >, std::allocator<char> >::close<boost::iostreams::detail::linked_streambuf<char, std::char_traits<char> > >(boost::iostreams::detail::linked_streambuf<char, std::char_traits<char> >&, std::_Ios_Openmode) in ods_v2-DjDcji.o
      void boost::iostreams::symmetric_filter<boost::iostreams::detail::zlib_decompressor_impl<std::allocator<char> >, std::allocator<char> >::close<boost::iostreams::non_blocking_adapter<boost::iostreams::detail::linked_streambuf<char, std::char_traits<char> …
Run Code Online (Sandbox Code Playgroud)

c++ compression boost zlib

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

C++如何将数组插入unordered_map作为其键?

嗨我曾经有一个unordered_set来保存我的16个int数组,现在我需要再存储一个int作为它的存储桶.我想知道我是否可以将数组插入到我的unordered_set中,还是可以使用我以前使用的相同模板?

#include <unordered_set>
#include <array>

namespace std
{
    template<typename T, size_t N>
    struct hash<array<T, N> >
    {
        typedef array<T, N> argument_type;
        typedef size_t result_type;

        result_type operator()(const argument_type& a) const
        {
            hash<T> hasher;
            result_type h = 0;
            for (result_type i = 0; i < N; ++i)
            {
                h = h * 31 + hasher(a[i]);
            }
            return h;
        }
    };
}

std::unordered_set<std::array<int, 16> > closelist;

int main()
{
    std::array<int, 16> sn = {1,2,3,4,5,6,0,8,9,10,11,12,13,14,7,15};
    closelist.insert(sn);
}
Run Code Online (Sandbox Code Playgroud)

我可以改成它吗?

std::unordered_map<std::array<int, 16>,int > closelist;

    int main() …
Run Code Online (Sandbox Code Playgroud)

c++ unordered-map c++11

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

C++如何读取带分隔符的行直到每行结束?

嗨,我需要读一个看起来像这样的文件......

1|Toy Story (1995)|Animation|Children's|Comedy
2|Jumanji (1995)|Adventure|Children's|Fantasy
3|Grumpier Old Men (1995)|Comedy|Romance
4|Waiting to Exhale (1995)|Comedy|Drama
5|Father of the Bride Part II (1995)|Comedy
6|Heat (1995)|Action|Crime|Thriller
7|Sabrina (1995)|Comedy|Romance
8|Tom and Huck (1995)|Adventure|Children's
9|Sudden Death (1995)|Action
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,每部电影的类型可以从1种变化到很多......我想知道在每行结束之前我怎么能读到这些?

我现在正在做:

void readingenre(string filename,int **g)
{

    ifstream myfile(filename);
    cout << "reading file "+filename << endl;
    if(myfile.is_open())
    {
        string item;
        string name;
        string type;
        while(!myfile.eof())
        {
            getline(myfile,item,'|');
            //cout <<item<< "\t";
            getline(myfile,name,'|');
            while(getline(myfile,type,'|'))
            {
                cout<<type<<endl;
            }
            getline(myfile,type,'\n');
        }
        myfile.close();
        cout << "reading genre file finished" <<endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

结果不是我想要的......它看起来像: …

c++ getline

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

如何将tr1/array转换为std :: string?

我不知道怎么投std::tr1::array<unsigned char, 16>std::string

编译器总是抱怨,我试过了

std::tr1::array<unsigned char, 16> sss;
string(sss);
string asd(sss);
Run Code Online (Sandbox Code Playgroud)

要么工作......

c++ string casting

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

C++如何每10秒检查一次时间?

我正在写一个检查点.我每次运行循环时都会检查.我认为这会浪费大量的CPU时间.我想知道如何每10秒检查一次系统时间?

time_t start = clock();
while(forever)
{
    if(difftime(clock(),start)/CLOCKS_PER_SEC >=timeLimit)
    {
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ time

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

为什么这个代码不能将std :: array转换为std :: string而不显示任何内容?

我正在使用此模板将我更改std::array为字符串.为什么不打印出任何东西?

#include <iostream>
#include <string>
#include <array>

template<std::size_t N>
std::string to_string_2(std::array<char, N> const& arr) {
  const char* str = reinterpret_cast<const char*>(arr.data());
  return std::string( str, str+N );
}
int main()
{
   std::array<char, 16> state = {1,2,3,4,5,6,7,8,9,0,11,12,13,14,15,0};
   std::cout << to_string_2(state) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays string casting

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

std :: ofstream :: open会将整个文件读入内存吗?

我正在把记忆中的东西写到磁盘上以释放我的记忆.

我想知道每次调用open(),并将appendix新元素放到文件末尾,它会将整个文件读入内存吗?或者它只是指向文件末尾的指针?

c++ memory fstream

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

如何将PHP数组插入SQL数据库

我试图将php数组插入sql数据库,如下所示:

$i=1    
$sql="INSERT INTO table(itemname) VALUES ('$_SESSION[someVariable][$i]')";
Run Code Online (Sandbox Code Playgroud)

但是,数据库中的名称始终是Array[1].它不是包含的价值$_SESSION[someVariable][$i]

我想知道是否还有声明这个?我知道我搞砸了报价

php mysql sql-server arrays

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

什么是std :: tr1 :: array <int,16 >>的大小

我想知道如何获得大小std::tr1::array<int, 16> >

它只是16*sizeof(int),这是如何16字节?

c++ sizeof

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