假设我有以下代码和输出:
for (j = 0; j <= i; j++)
printf("substring %d is %s\n", j, sub_str[j]);
Run Code Online (Sandbox Code Playgroud)
输出:
substring 0 is max_n=20 substring 1 is max_m=20
现在我只想打印一些子串.但是,如果我尝试有条件地这样做:
for (j=0; j <=i; j++) {
if (sub_str[j] == "max_n=20") {
printf("substring %d is %s\n", j, sub_str[j]);
}
}
Run Code Online (Sandbox Code Playgroud)
我根本没有输出.我的代码出了什么问题?
让我们拿一些代码示例:
! 4 > 0;
Run Code Online (Sandbox Code Playgroud)
从C++标准我们知道,否则将首先进行否定,而不是比较.但是,如果我们稍微扩展这个例子:
#include <iostream>
class Test
{
public:
bool operator!() const
{
std::cout << "operator!" << std::endl;
return false;
}
bool operator>(const int &) const
{
std::cout << "operator>" << std::endl;
return false;
}
};
int main(int argc, char * argv[])
{
Test t;
std::cout << "t > 0;" << std::endl;
t > 0;
std::cout << "! t > 0;" << std::endl;
! t > 0;
std::cout << "!t.operator>(0)" << std::endl;
! t.operator>(0);
return 0;
} …Run Code Online (Sandbox Code Playgroud) 为什么这段代码不能编译?
struct A {
template <class T>
static T a(int i) { return 2*i; }
};
template <class T>
struct B {
double b;
B(): b(T::a<double>(5)) {}
};
template class B<A>;
Run Code Online (Sandbox Code Playgroud)
编译器甚至没有到达模板实例化.我正在使用gcc 4.7.0.
test.cc: In constructor »B<T>::B()«:
test.cc:9:25: Error: expected »(« before »<« token
test.cc:9:26: Error: expected primary-expression before »double«
Run Code Online (Sandbox Code Playgroud) Qt 的 QStyle 提供了standardIcon,它可以在给定 StandardPixmap 值的情况下抓取标准图标:
const QStyle * style = QApplication::style();
const QIcon ok = style->standardIcon(QStyle::SP_DialogOkButton);
Run Code Online (Sandbox Code Playgroud)
如果你想手动设置图标,这很好.setIcon,例如在 QPushButtons 或其他小部件上。但是,我想在(外部)样式表中设置图标。如果图标在 中可用:/images/ok.icon,那将很容易:
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton button("Example");
button.setStyleSheet(
"icon-size: 32px 32px;"
"qproperty-icon: url(:/images/ok.icon);" // <--
);
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
但是,我想使用style->standardIcon(QStyle::SP_DialogOkButton);,而不是资源系统中的图标。这可能吗,还是我需要手动准备所有图标?
// What I have:
button.setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogOkButton));
// What I want:
button.setStyleSheet(
"qproperty-icon: url(some_url);"
);
Run Code Online (Sandbox Code Playgroud) 我试图在Haskell中正确读取文件,但我似乎得到了这个错误.
***例外:neo.txt:openFile:资源忙(文件被锁定)这是我的代码.
import Data.Char
import Prelude
import Data.List
import Text.Printf
import Data.Tuple
import Data.Ord
import Control.Monad
import Control.Applicative((<*))
import Text.Parsec
( Parsec, ParseError, parse -- Types and parser
, between, noneOf, sepBy, many1 -- Combinators
, char, spaces, digit, newline -- Simple parsers
)
Run Code Online (Sandbox Code Playgroud)
这些是电影领域.
type Title = String
type Director = String
type Year = Int
type UserRatings = (String,Int)
type Film = (Title, Director, Year , [UserRatings])
type Period = (Year, Year)
type Database = [Film]
Run Code Online (Sandbox Code Playgroud)
这是所有类型的解析,以便从文件中正确读取
-- …Run Code Online (Sandbox Code Playgroud) 我是编程新手.我有一个问题,我自己找不到一个可以理解的答案.我想通过使用C++和C找到指针的地址,但是两个结果是不同的,尽管它们有一些相似的数字.他们还是同一个地址吗?
adress of g is :0018F9C4
address of g is: 0018F9D3
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#include<iostream>
#include<stdio.h>
void main()
{
char g = 'z';
char*p;
p = &g;
std::cout << "adress of g is :" << &p;
printf("\naddress of g is: %p", p);
}
Run Code Online (Sandbox Code Playgroud) HLint给出了如何改进源代码的建议.但是,鉴于建议的性质,我想知道是否可以自动应用这些建议.
是否可以自动应用提出的建议hlint?
我有一个类型,Foo并希望使它成为一个实例Show,以便我可以在GHCi中使用它:
data Foo = Foo
instance Show Foo where
show Foo = "Foo"
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用它时,出现了一个模糊的错误:
ghci> show Foo
<interactive>:4:1:
Ambiguous occurrence `show'
It could refer to either `Main.show', defined at Foo.hs:4:1
or `Prelude.show',
imported from `Prelude' at Foo.hs:1:1
(and originally defined in `GHC.Show')
Run Code Online (Sandbox Code Playgroud)
为什么?我刚刚定义了属于类型类的函数,不是吗?
我正在开发用react-native编写的字典应用程序。
当我想从搜索框中过滤数组时,我编写了以下函数。当我用 2000 个单词列表进行测试时,效果非常好。但是当单词列表达到数千个时,搜索速度就非常慢了。
那么,如何改进这个搜索功能呢?
//Filter array when input text (Search)
let filteredWords = []
if(this.state.searchField != null)
{
filteredWords = this.state.glossaries.filter(glossary => {
return glossary.word.toLowerCase().includes(this.state.searchField.toLowerCase());
})
}
Run Code Online (Sandbox Code Playgroud) window.TicTacToet.compMove = function (row, col) {
var player = window.TicTacToet.PlayerTurn;
var board = window.TicTacToet.Board;
for (i = 0; i < window.TicTacToet.Board.length; i++) {
for (j = 0; j < window.TicTacToet.Board[i].length; j++) {
if (window.TicTacToet.Board[i][j] == null) {
getWin(row, col, player, board);
} else {
console.log("position occupied");
}
}
}
}
function getWin($r, $c, $player, $board) {
checkTop($r, $c, $player, $board);
}
function checkTop($x, $y, $player, b) {
console.log("ENTER");
var success = false;
for (i = 0; i < 3; i++) …Run Code Online (Sandbox Code Playgroud) c++ ×3
haskell ×3
javascript ×2
arrays ×1
c ×1
comparison ×1
exception ×1
expo ×1
file ×1
hlint ×1
html ×1
locked ×1
negation ×1
qt ×1
react-native ×1
refactoring ×1
search ×1
syntax ×1
templates ×1
typeclass ×1