std :: wstring的问题 [英] Problem with std::wstring

查看:116
本文介绍了std :: wstring的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,下面的代码在VS 2013下编译并完美运行但是当我尝试在Linux机器上使用g ++编译它时,我得到了一些关于std :: wstring的错误,我知道这就是问题,好像我用string和ostringstream分别替换所有wstring和wostringstream都很好。这只是个人项目,因此目前非常粗糙。



Hi all, the code below compiles and runs perfectly under VS 2013 but when I try and compile it using g++ on a Linux box I get a slew of errors regarding std::wstring, I know this is the problem as if I replace all wstring and wostringstream with string and ostringstream respectively all is good. This is only a personal project and thus very rough at the moment.

#include <vector>
#include <string>
#include <time.h>
#include <iostream>
#include <sstream>
#include <map>
#include "squeezemessage.h"
#include <random>

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>

using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams

int ResultCount = 0;
std::vector<std::pair<std::wstring, std::wstring>> Artists;

void GetJsonResponse(json::value &jsonValue, http_response &response)
{
	jsonValue = response.extract_json().get().at(U("result"));
	ResultCount = jsonValue.size();
}

void SetRequest(http_request &requester, std::wstring &SqueezeMsg)
{
	requester.set_method(methods::POST);
	requester.headers().set_content_type(L"application/json");
	requester.set_body(SqueezeMsg);
}

std::wstring GetClient()
{
	return L"http://10.10.1.11:9000/jsonrpc.js";
}


pplx::task<void> Post(std::wstring SqueezeMsg)
{
	http_request requester;
	SetRequest(requester, SqueezeMsg);
	Artists.clear();

	return pplx::create_task([requester]
	{
		std::wstring URL(GetClient());

		http_client client(URL);

		return client.request(requester);

	}).then([](http_response response)
	{
		int code = response.status_code();

		if (response.status_code() == status_codes::OK)
		{
			json::value jsonValue;
			GetJsonResponse(jsonValue, response);

			std::map<std::wstring, std::wstring> Entities;

			Entities.insert(std::pair<std::wstring, std::wstring>(U("contributors_loop"),U("contributor")));
			Entities.insert(std::pair<std::wstring, std::wstring>(U("albums_loop"),U("album")));
			Entities.insert(std::pair<std::wstring, std::wstring>(U("tracks_loop"),U("track")));	
			
			std::wostringstream ss;

			if (ResultCount > 0)
			{
				ResultCount = 0;

				std::map<std::wstring, std::wstring>::iterator it;

				for (it = Entities.begin(); it != Entities.end(); ++it)
				{
					if (jsonValue.has_field(it->first))
					{
						json::value EntityArray = jsonValue.at(it->first);
						std::wstring FieldName(it->second);

						for (size_t i = 0; i < EntityArray.size(); i++)
						{
							std::wostringstream ss;
							ss << FieldName << ": " << (i + 1);
							std::wstring whatever(EntityArray[i].at(FieldName).as_string());
							Artists.push_back(std::pair<std::wstring, std::wstring>(ss.str(), whatever));
							ResultCount++;
						}
					}
				}
			}
		}
	});
}

int main(int argc, char* argv[])
{
	if (argc < 2)
	{
		std::wcout << L"\n\nUsage: " << argv[0] << L" search term\n";
		return -1;
	}

	SqueezeMessage Squeezer;
	std::wostringstream ss;
	ss << L"term:" << argv[1];
	
	std::wstring SearchTerm(ss.str());

	Squeezer.BuildMessage(L"search", L"0", L"10000", SearchTerm);
	std::wstring SqueezeMsg = Squeezer.SqueezeRequestW;
	std::wcout << L"\n\n*** Running query " << SqueezeMsg << L"\n";

	Post(SqueezeMsg).get();

	for (std::vector<std::pair<std::wstring, std::wstring>>::iterator it = Artists.begin(); it != Artists.end(); ++it)
		std::wcout << it->first << ":" << it->second << '\n';

	std::wcout << L"\n\n*** " << Artists.size() << L" row" << (Artists.size() == 1 ? L"" : L"s") << L" returned ***\n";
}





我的尝试:



谷歌搜索,大量饮酒,长途跋涉(到酒吧和后面),最后在这里发帖。



What I have tried:

Googling, drinking heavily, long walks ( to the pub and back ) and finally posting here.

推荐答案

通常没有必要使用<使用Linux的code> std :: wstring 因为它多年来默认使用UTF-8字符串。因此,您可以使用 char 字符串获得完整的Unicode支持。



在Linux上使用 std:wstring 时总会遇到问题,因为几乎所有库函数都需要 char 字符串。



[更新]

来自FAQ·Microsoft / cpprestsdk Wiki·GitHub [ ^ ]:

There is usually no need to use std::wstring with Linux because it uses UTF-8 strings by default since many years. So you have full Unicode support with char strings.

You will always run into problems when using std:wstring with Linux because nearly all library functions expect char strings.

[UPDATE]
From FAQ · Microsoft/cpprestsdk Wiki · GitHub[^]:
引用:

C ++ REST SDK使用不同的字符串类型,具体取决于所针对的平台。例如对于Windows平台实用程序:: string_t是使用UTF-16的std :: wstring,在Linux std :: string上使用UTF-8。

The C++ REST SDK uses a different string type dependent on the platform being targeted. For example for the Windows platforms utility::string_t is std::wstring using UTF-16, on Linux std::string using UTF-8.



所以你可以使用 utility :: string_t 改为输入。

[/ UPDATE]


So you might use the utility::string_t type instead.
[/UPDATE]


这篇关于std :: wstring的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆