我想检查boost::variant在我的代码中应用的程序集输出,以便查看哪些中间调用被优化掉了.
当我编译以下示例(使用GCC 5.3 g++ -O3 -std=c++14 -S)时,似乎编译器优化了所有内容并直接返回100:
(...)
main:
.LFB9320:
.cfi_startproc
movl $100, %eax
ret
.cfi_endproc
(...)
Run Code Online (Sandbox Code Playgroud)
#include <boost/variant.hpp>
struct Foo
{
int get() { return 100; }
};
struct Bar
{
int get() { return 999; }
};
using Variant = boost::variant<Foo, Bar>;
int run(Variant v)
{
return boost::apply_visitor([](auto& x){return x.get();}, v);
}
int main()
{
Foo f;
return run(f);
}
Run Code Online (Sandbox Code Playgroud)
但是,完整的程序集输出包含的内容远远超过上面的摘录,对我而言,它看起来永远不会被调用.有没有办法告诉GCC/clang删除所有"噪音"并输出程序运行时实际调用的内容?
完整装配输出:
.file "main1.cpp"
.section .rodata.str1.8,"aMS",@progbits,1
.align 8
.LC0:
.string "/opt/boost/include/boost/variant/detail/forced_return.hpp"
.section .rodata.str1.1,"aMS",@progbits,1
.LC1: …Run Code Online (Sandbox Code Playgroud) 我遇到了下面的代码,但需要一些帮助来理解代码.假设字符串s在任一侧都有空格.
string trim(string const& s){
auto front = find_if_not(begin(s), end(s), isspace);
auto back = find_if_not(rbegin(s), rend(s), isspace);
return string { front, back.base() };
}
Run Code Online (Sandbox Code Playgroud)
作者说,后面指向最后一个空格的末尾,而前面指向第一个非空白字符.所以back.base()被调用但我不明白为什么.
另外,在return语句中跟随字符串的花括号是什么?
我使用std::ptr_fun如下:
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
Run Code Online (Sandbox Code Playgroud)
如本回答所示.
但是这不能用C++ 17编译(使用Microsoft Visual Studio 2017),错误如下:
error C2039: 'ptr_fun': is not a member of 'std'
Run Code Online (Sandbox Code Playgroud)
怎么解决这个问题?
我输入了这个,它要求用户输入两个整数,然后变成变量.从那里它将执行简单的操作.
如何让计算机检查输入的内容是否为整数?如果没有,请让用户键入一个整数.例如:如果有人输入"a"而不是2,那么它会告诉他们重新输入一个数字.
谢谢
#include <iostream>
using namespace std;
int main ()
{
int firstvariable;
int secondvariable;
float float1;
float float2;
cout << "Please enter two integers and then press Enter:" << endl;
cin >> firstvariable;
cin >> secondvariable;
cout << "Time for some simple mathematical operations:\n" << endl;
cout << "The sum:\n " << firstvariable << "+" << secondvariable
<<"="<< firstvariable + secondvariable << "\n " << endl;
}
Run Code Online (Sandbox Code Playgroud) 您可以使用逗号/制表符/任何带分隔符的文本文件计算每行的字段数utils::count.fields.
这是一个可重复的例子:
d <- data.frame(
x = c(1, NA, 3, NA, 5),
y = c(NA, "b", "c", NA, NA),
z = c(NA, "beta", "gamma", NA, "epsilon")
)
fname <- "test.csv"
write.csv(d, fname, na = "", row.names = FALSE)
count.fields(fname, sep = ",")
## [1] 3 3 3 3 3 3
Run Code Online (Sandbox Code Playgroud)
我想计算每行的非空字段数.我可以通过读取所有内容并计算不是的值的数量,以笨重的方式做到这一点NA.
d2 <- read.csv(fname, na.strings = "")
rowSums(!is.na(d2))
## [1] 1 2 3 0 2
Run Code Online (Sandbox Code Playgroud)
我真的很喜欢扫描文件的方式(比如count.fields),所以我可以针对特定的部分进行读取.
有没有更好的方法来计算分隔文件中的非空字段数?
我想首先说是的,我来这里之前谷歌已经解决了这个问题,但没有一个答案似乎有用.
我偷了下面的代码,什么是修剪std :: string的最佳方法?,因为无论什么原因,没有标准的c ++ trim函数.使用Visual Studio,它编译得很好,我设法完成项目的其余部分,而不会给我任何错误.
今天早上,我决定尝试手动编译整个项目(使用g++ -std=c++11 *.cpp),现在突然出现这些trim函数产生以下错误:
DVD.cpp: In static member function 'static DVD DVD::parseDVD(std::string, std::string)':
DVD.cpp:65:59: error: invalid initialization of non-const reference of type 'std::string& {aka std::basic_string<char>&}
from an rvalue of type 'std::basic_string<char>'
std::string rID = trim(dataStr.substr(0, preTitlePos - 1));
Run Code Online (Sandbox Code Playgroud)
对于使用的其他2次,它产生类似的错误trim.
这是"被盗"的代码:
(Utils.h):
static inline std::string& ltrim(std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string& rtrim(std::string& s) { …Run Code Online (Sandbox Code Playgroud) 我想删除引用字符串传递的第一个和最后一个括号.不幸的是,我有条件地删除第一个和最后一个元素.我无法理解为什么remove_if不能像迭代器那样工作.
#include <iostream>
#include <algorithm>
using namespace std;
void print_wo_brackets(string& str){
auto detect_bracket = [](char x){ return(')' == x || '(' == x);};
if (!str.empty())
{
str.erase(std::remove_if(str.begin(), str.begin() + 1, detect_bracket));
}
if (!str.empty())
{
str.erase(std::remove_if(str.end()-1, str.end(), detect_bracket));
}
}
int main()
{
string str = "abc)";
cout << str << endl;
print_wo_brackets(str);
cout << str << endl;
string str2 = "(abc";
cout << str2 << endl;
print_wo_brackets(str2);
cout << str2 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Python中有一个非常有用的函数叫做strip().C++中有类似的吗?
我偶然发现了string :: substr的奇怪行为.通常我在Eclipse + MinGW 上的Windows 7上编码,但是当我在笔记本电脑上工作时,在Linux中使用Eclipse (Ubuntu 12.04),我发现结果有所不同.
我正在使用填充了文本行的vector <string>.其中一个步骤是从行中删除最后一个字符.
在win7 Eclipse中我做了:
for( int i = 0; i < (int)vectorOfLines.size(); i++ )
{
vectorOfTrimmedLines.push_back( ((string)vectorOfLines.at(i)).substr(0, ((string)vectorOfLines.at(i)).size()-1) );
}
Run Code Online (Sandbox Code Playgroud)
它的工作方式与预期相同(从每行中删除最后一个字符)
但在Linux中,这段代码不会修剪.相反,我需要像这样做:
// -2 instead -1 character
vectorOfTrimmedLines.push_back( ((string)vectorOfLines.at(i)).substr(0, ((string)vectorOfLines.at(i)).size()-2) );
Run Code Online (Sandbox Code Playgroud)
或使用其他方法:
vectorOfTrimmedLines.push_back( ((string)vectorOfLines.at(i)).replace( (((string)vectorOfLines.at(i)).size()-2),1,"",0 ));
Run Code Online (Sandbox Code Playgroud)
当然Linux方法在Windows上工作错误(修剪2个最后一个字符,或者在最后一个字符之前替换一个字符).
问题似乎是myString.size()返回Windows中的字符数,但在Linux中它返回的字符数为+ 1.可能是Linux上的新行字符?
作为C++和编程通用的新手,我想知道为什么会这样,以及如何做到平台无关.
我想知道的另一件事是:哪种方法更好(更快)substr或替换?
编辑:用于填充字符串的方法我写的这个函数:
vector< string > ReadFile( string pathToFile )
{
// opening file
ifstream myFile;
myFile.open( pathToFile.c_str() );
// …Run Code Online (Sandbox Code Playgroud) 有没有办法特别从字符串的开头和结尾删除标点符号,同时单独留下收缩和占有欲?
例如"!哇!" 会变成"哇"而"不能"会留下"不能"
我有一个名为“items.dat”的文件,在订单 itemID、itemPrice 和 itemName 中有以下内容。
item0001 500.00 item1 name1 with spaces
item0002 500.00 item2 name2 with spaces
item0003 500.00 item3 name3 with spaces
Run Code Online (Sandbox Code Playgroud)
我编写了以下代码来读取数据并将其存储在结构中。
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
struct item {
string name;
string code;
double price;
};
item items[10];
void initializeItem(item tmpItem[], string dataFile);
int main() {
initializeItem(items, "items.dat");
cout << items[0].name << endl;
cout << items[0].name.at(1) << endl;
return 0;
}
void initializeItem(item tmpItem[], string dataFile) {
ifstream fileRead(dataFile); …Run Code Online (Sandbox Code Playgroud) 我编写了以下 C++ 代码,它从字符串的开头和结尾删除空格,但问题是它没有删除制表符,我该如何解决?
另外,有没有类似于制表符和空格的东西?(我正在从文件中读取行)
string trim_edges(string command) {
const auto command_begin = command.find_first_not_of(' ');
if (command_begin == std::string::npos)
return "";
const auto strEnd = command.find_last_not_of(' ');
const auto strRange = strEnd - command_begin + 1;
return command.substr(command_begin, strRange);
}
Run Code Online (Sandbox Code Playgroud) 我的代码旨在告诉用户输入的字符串是否是c ++中的关键字.我正在从文件中读取关键字到集合中,然后检查用户提供的字符串是否在其中.
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <fstream>
using namespace std;
int main()
{
set<string> key;
fstream fs;
string b;
fs.open("keywords.txt",fstream::in);
while(getline(fs,b))
key.insert(b);
b.clear();
for(auto x:key)
cout << x << endl;
cout << "Enter String user\nPress exit to terminate\n";
while(getline(cin,b))
{
if(b == "exit")
break;
if(key.find(b) != key.end())
cout << "This is a keyword\n";
else
cout << "This is a not a keyword\n";
b.clear();
}
fs.close();
}
Run Code Online (Sandbox Code Playgroud)
keywords.txt文件只是一个关键字列表,可以从这里获得
问题是我的程序正确读取所有关键字,但对于其中一些如false,public它无法在集合中找到它们.
即,当我输入false作为用户输入时,它说"这不是关键字".