- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
虽然我可以通过在 std::sort 中指定 UTF-8 语言环境来成功地对带有重音元音的西类牙语单词进行排序,
// [[Rcpp::export]]
std::vector<std::string> sort_words(std::vector<std::string> x) {
std::sort(x.begin(), x.end(), std::locale("en_US.UTF-8"));
return x;
}
/*** R
words <- c("casa", "árbol", "zona", "árbol", "casa", "libro")
sort_words(words)
*/
returns (as expected):
[1] "árbol" "árbol" "casa" "casa" "libro" "zona"
我不知道如何用 map 做同样的事情:
// slightly modified version of tableC on http://adv-r.had.co.nz/Rcpp.html
// [[Rcpp::export]]
std::map<String, int> table_words(CharacterVector x) {
std::setlocale(LC_ALL, "en_US.UTF-8");
// std::setlocale(LC_COLLATE, "en_US.UTF-8"); // also tried this instead of previous line
std::map<String, int> counts;
int n = x.size();
for (int i = 0; i < n; i++) {
counts[x[i]]++;
}
return counts;
}
/*** R
words <- c("casa", "árbol", "zona", "árbol", "casa", "libro")
table_words(words)
*/
returns:
casa libro zona árbol
2 1 1 2
but I want:
árbol casa libro zona
2 2 1 1
关于如何让 table_words
使用 Rcpp 将带重音的“árbol”放在“casa”之前,或者甚至在 R 中使用 base::sort
退出,有什么想法吗?
此外,std::sort(..., std::locale("en_US.UTF-8"))
在我的 Linux 机器上只有文字:gcc 版本 4.8.2(Ubuntu 4.8.2-19ubuntu1)。它不适用于 Mac 10.10.3:Apple LLVM 版本 6.1.0 (clang-602.0.53)(基于 LLVM 3.6.0svn)。关于我的 Mac 编译器缺少而我的 Linux 编译器有什么的任何线索?
对于两台机器,这是我的脚本和我的 session 信息:
// [[Rcpp::plugins(cpp11)]]
#include <locale>
#include <clocale>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
std::vector<std::string> sort_words(std::vector<std::string> x) {
std::sort(x.begin(), x.end(), std::locale("en_US.UTF-8"));
return x;
}
// [[Rcpp::export]]
std::map<String, int> table_words(CharacterVector x) {
// std::setlocale(LC_ALL, "en_US.UTF-8"); // tried this instead of next line
std::setlocale(LC_COLLATE, "en_US.UTF-8");
std::map<String, int> counts;
int n = x.size();
for (int i = 0; i < n; i++) {
counts[x[i]]++;
}
return counts;
}
/*** R
words <- c("casa", "árbol", "zona", "árbol", "casa", "libro")
sort_words(words)
table_words(words)
sort(table_words(words), decreasing = T)
output_from_Rcpp <- table_words(words)
sort(names(output_from_Rcpp))
*/
> words <- c("casa", "árbol", "zona", "árbol", "casa", "libro")
> sort_words(words)
[1] "árbol" "árbol" "casa" "casa" "libro" "zona"
> table_words(words)
casa libro zona árbol
2 1 1 2
> sort(table_words(words), decreasing = T)
casa árbol libro zona
2 2 1 1
> output_from_Rcpp <- table_words(words)
> sort(names(output_from_Rcpp))
[1] "árbol" "casa" "libro" "zona"
sessionInfo on linux machine:
R version 3.2.0 (2015-04-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04 LTS
locale:
[1] en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.2.0 Rcpp_0.11.6
sessionInfo on Mac:
R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)
locale:
[1] en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] textcat_1.0-3 readr_0.1.1 rvest_0.2.0
loaded via a namespace (and not attached):
[1] httr_1.0.0 selectr_0.2-3 R6_2.1.0 magrittr_1.5 tools_3.2.1 curl_0.9.1 Rcpp_0.11.6 slam_0.1-32 stringi_0.5-5
[10] tau_0.0-18 stringr_1.0.0 XML_3.98-1.3
最佳答案
在 std::map
上应用 std::sort
没有意义,因为 map 总是排序,根据定义。该定义是模板实例化的具体类型的一部分。 std::map
具有第三个“隐藏”类型参数,用于用于对键进行排序的比较函数,键类型默认为 std::less
。参见 http://en.cppreference.com/w/cpp/container/map .
在你的情况下,你可以使用 std::locale
作为比较类型,并传递 std::locale("en-US")
(或任何合适的你的系统)到构造函数。
这是一个例子。它使用 C++11,但您可以在 C++03 中轻松使用相同的解决方案。
#include <map>
#include <iostream>
#include <string>
#include <locale>
#include <exception>
using Map = std::map<std::string, int, std::locale>;
int main()
{
try
{
Map map(std::locale("en-US"));
map["casa"] = 1;
map["árbol"] = 2;
map["zona"] = 3;
map["árbol"] = 4;
map["casa"] = 5;
map["libro"] = 6;
for (auto const& map_entry : map)
{
std::cout << map_entry.first << " -> " << map_entry.second << "\n";
}
}
catch (std::exception const& exc)
{
std::cerr << exc.what() << "\n";
}
}
输出:
árbol -> 4
casa -> 5
libro -> 6
zona -> 3
当然,您必须意识到 std::locale
高度依赖于实现这一事实。 Boost.Locale 你可能会过得更好.
另一个问题是这个解决方案可能看起来令人困惑,因为 std::locale
并不是许多程序员会与比较函数相关联的东西。这几乎有点太聪明了。
因此一个可能更具可读性的替代方案:
#include <map>
#include <iostream>
#include <string>
#include <locale>
#include <exception>
struct ComparisonUsingLocale
{
std::locale locale{ "en-US" };
bool operator()(std::string const& lhs, std::string const& rhs) const
{
return locale(lhs, rhs);
}
};
using Map = std::map<std::string, int, ComparisonUsingLocale>;
int main()
{
try
{
Map map;
map["casa"] = 1;
map["árbol"] = 2;
map["zona"] = 3;
map["árbol"] = 4;
map["casa"] = 5;
map["libro"] = 6;
for (auto const& map_entry : map)
{
std::cout << map_entry.first << " -> " << map_entry.second << "\n";
}
}
catch (std::exception const& exc)
{
std::cerr << exc.what() << "\n";
}
}
关于c++ - 在 Rcpp 中用(西类牙语)重音词对 map 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31498002/
这个问题已经有答案了: What is the Java ?: operator called and what does it do? (17 个回答) 已关闭 8 年前。 ltVal = node
我是一名 Java 学生,我在嵌套该程序的条件语句时遇到问题 Exercise CozaLozaWoza (Loop & Condition): Write a program called Coza
首先,我想给出用户想要留下的句子的数量,当他的写作结束时,我的代码开始将每个单词的第一个字母大写(在 Java 中)。 import java.util.Scanner; public class I
我尝试在基类中实现一个函数,该函数使用子函数(defiend 作为基类中的抽象函数)。我认为一个例子可以最好地说明这个问题。 abstract class Animal{ public void
就像在口吃中一样,如果文本为“dean”并且乘数为 3,则结果将是“dddeeeaaannn”。 public static void repeatLetters() { String text
public void insert(int data) { if (root == null) root = new AVLNode(data); else {
我是 XPATH 的新手,并且遇到以下问题: 我有以下代码片段,但似乎无法按我的预期工作: String XML = cdataContent;
例如,Java 数据类型字节将数据从 -128 到 127 存储在单个字节中。为了能够区分 - 1 到 -128 从 0 到 127 将需要额外的数据,这些数据将采用数据类型覆盖其分配的存储空间。不可
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
Dataset: P1: Lion, Snow, Chair P2: Min: 0, Max: 28 P3: Min: 34, Max is 39. 我的程序 以一系列数组列表的形式提供上述数据集(P
我正在构建一个应该 24/7 全天候运行的客户端服务器应用程序。应用程序指定检测网络故障(使用心跳)并尽快重新连接到服务器。 我做的第一个测试只是停止客户端或服务器,然后重新启动,一切正常。我想知道是
我怀疑它是编写它的类的类型,但我不是 100% 确定,有人可以证实我的怀疑并可能提供对定义此行为的 Java 语言规范的引用吗? 假设类 A 有一个方法 a(),它在其主体中使用了 this 关键字,
我已经在谷歌上搜索了两个小时,但没有成功。 如果我有一个模板函数并且我想在模板类型上强制执行一个接口(interface),我该怎么做? 例如。 void doStuff(T)(bool param)
我正在尝试获取用户输入并对其进行修改,以便打印不带任何元音的字符串。我已经能够使用以下代码成功完成此操作。 Scanner in = new Scanner(System.in); Syste
每当我使用 Thread.sleep(); 时在 do while 循环中,提示告诉我,“在循环中调用 Thread.sleep 可能会导致性能问题。”我从许多其他网站和书籍上听到过这一点。我可以用什
请不要将其视为以下内容的重复项而将其忽略: How to generate random positive and negative numbers in java 我需要使用带有种子的随机数生成器。
我想在一个数字范围内选择随机数,但权重偏向该范围的一部分。例如: 选择1-10之间的随机数 对其进行加权,使 1-5 比 6-10 的可能性高 20% 这可能吗?我该怎么做? 最佳答案 这取决于您希望
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我有一个付款 Activity 和启动 Activity ,它在用户购买后显示内容应用程序。付款 Activity 是Manifest.xml中的默认启动器,我想将启动器 Activity 设置为启动
我有一个指针和长度。如何从他们那里得到一个动态数组? 最佳答案 设ptr是一个指针,len是一个长度,那么很容易如下: ptr[0..len] 请注意,这不会复制数组,而是就地使用数据。 如果要复制数
我是一名优秀的程序员,十分优秀!