Snippets Collections
#include <vector>
#include <iostream>

template<class V, class S>
void read_numbers(V& v, S& s, N end)
{
    N x=0;
    while(x << s)
    {
        if(x==end) return;
        v.push_back(x);
    }
    std::cerr << "error: bad reading" << std::endl;
}

template<class V, class S>
void write_numbers(const V& v, S& s)
{
    for(auto i=s.rbegin(); i!=s.rend(); ++i)
        std::cout << *i << ' ';
    std::cout << std::endl;
}

int main()
{
    std::vector<int> nums;
    read_numbers(nums, std::cin, -1); }
    write_numbers(nums, std::cout);
    return 0;
}
#import "UIPlaceHolderTextView.h"

@interface UIPlaceHolderTextView ()

@property (nonatomic, retain) UILabel *placeHolderLabel;

@end

@implementation UIPlaceHolderTextView

CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25;

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
#if __has_feature(objc_arc)
#else
    [_placeHolderLabel release]; _placeHolderLabel = nil;
    [_placeholderColor release]; _placeholderColor = nil;
    [_placeholder release]; _placeholder = nil;
    [super dealloc];
#endif
}

- (void)awakeFromNib
{
    [super awakeFromNib];

    // Use Interface Builder User Defined Runtime Attributes to set
    // placeholder and placeholderColor in Interface Builder.
    if (!self.placeholder) {
        [self setPlaceholder:@""];
    }

    if (!self.placeholderColor) {
        [self setPlaceholderColor:[UIColor lightGrayColor]];
    }

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
}

- (id)initWithFrame:(CGRect)frame
{
    if( (self = [super initWithFrame:frame]) )
    {
        [self setPlaceholder:@""];
        [self setPlaceholderColor:[UIColor lightGrayColor]];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

- (void)textChanged:(NSNotification *)notification
{
    if([[self placeholder] length] == 0)
    {
        return;
    }

    [UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{
    if([[self text] length] == 0)
    {
        [[self viewWithTag:999] setAlpha:1];
    }
    else
    {
        [[self viewWithTag:999] setAlpha:0];
    }
    }];
}

- (void)setText:(NSString *)text {
    [super setText:text];
    [self textChanged:nil];
}

- (void)drawRect:(CGRect)rect
{
    if( [[self placeholder] length] > 0 )
    {
        if (_placeHolderLabel == nil )
        {
            _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
            _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
            _placeHolderLabel.numberOfLines = 0;
            _placeHolderLabel.font = self.font;
            _placeHolderLabel.backgroundColor = [UIColor clearColor];
            _placeHolderLabel.textColor = self.placeholderColor;
            _placeHolderLabel.alpha = 0;
            _placeHolderLabel.tag = 999;
            [self addSubview:_placeHolderLabel];
        }

        _placeHolderLabel.text = self.placeholder;
        [_placeHolderLabel sizeToFit];
        [self sendSubviewToBack:_placeHolderLabel];
    }

    if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
    {
        [[self viewWithTag:999] setAlpha:1];
    }

    [super drawRect:rect];
}

@end
enum InputMode {
	IntegerInput = 0, StringInput
};


std::string InputValidity(int mode) {

	std::string input;

	//bool invalid = true;

	while (true) {

		std::getline(cin, input);

		switch (mode)
		{
		case IntegerInput:

			int intTest;
			sscanf_s(input.c_str(), "%d\n", &intTest);

			if (intTest >= 2 && intTest <= 4) {
				return input;
			}
			else
			{
				cout << "Invalid input. The number of players must be 2-4.\n";
			}
			break;

		case StringInput:

			std::string strTest = input.substr(input.size() - 4);

			if (strTest == ".txt")
			{
				return input;
			}
			else {
				std::cout << "That input is invalid" << std::endl;
			}
			break;
		}
	}
}

int GetIntInput() {

	cout << "Please input the number of players (2-4): ";
	int returnedVal;
	std::string returnedString = InputValidity(0);
	sscanf_s(returnedString.c_str(), "%d\n", &returnedVal);
	cout << returnedVal << std::endl;

	return returnedVal;
}

std::string GetStrInput() {

	cout << "Please input the name of the board you wish to open (in the format \"<name>.txt\"): ";
	std::string returnedStr = InputValidity(1);
	return returnedStr;
}
double AttackerSuccessProbability(double q, int z)
{
    double p = 1.0 - q;
    double lambda = z * (q / p);
    double sum = 1.0;
    int i, k;
    for (k = 0; k <= z; k++)
    {
        double poisson = exp(-lambda);
        for (i = 1; i <= k; i++)
            poisson *= lambda / i;
        sum -= poisson * (1 - pow(q / p, z - k));
    }
    return sum;
}
star

Tue May 19 2020 10:47:25 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/29867381/how-to-make-a-list-c

#C++
star

Fri May 15 2020 19:19:19 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/1328638/placeholder-in-uitextview

#C++
star

Thu May 14 2020 00:41:12 GMT+0000 (Coordinated Universal Time)

#C++

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension