I need support for space, tab, and newline delimiters. I can do this with sscanf but I'd prefer to stick to strings rather than char*s.
Is there a way I can acomplish this in C++11?
How about this simple program:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
int main()
{
std::vector<std::string> v;
std::istringstream is{"example input\ttext\nhere"};
std::copy(std::istream_iterator<std::string>(is),
std::istream_iterator<std::string>(),
std::back_inserter(v));
for (const auto& s : v)
std::cout << s << '\n';
}
The output from this program is
example
input
text
here
As you can see it uses any whitespace as delimiter. You can of course use the input operator as well, like in
std::string s;
while (is >> s)
v.push_back(s);
The above instead of std::copy will produce the same result.
Related
Attempting to copy from a character vector to a string vector has been unsuccessful across multiple attempts at a solution
allocating memory to the vector prior to copying allows std::copy to work properly when placed at "OutputIterator result" (based on function template). I attempted:
std::copy(char1.begin(), char1.end(), v1.begin());
however, this was unsuccessful as well. using back_inserter returns error c2679 "binary '=': no operator found which takes a right-hand operand of type'char' (or there is no acceptable conversion).
input file is located here: https://adventofcode.com/2018/day/2
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstring>
#include <algorithm>
#include <iterator>
#include <cstdio>
int main() {
std::string field1;
std::string field2;
char characters;
std::vector<char>::iterator ptr;
std::vector<char>::iterator ptr2;
std::vector<char> char1;
std::vector<char> char2;
int size = 0;
std::ifstream inFile;
inFile.open("C:\\Users\\Administrator\\Desktop\\c++ files\\input2.txt");
if (!inFile) {
std::cout << "abort";
return 1;
}
while (inFile >> characters) { //read variables from input stream
char1.push_back(characters);
}
std::vector<std::string> v1(6500);
std::copy(char1.begin(), char1.end(), std::back_inserter(v1));
inFile.close();
return 0;
}
//26
expect vector v1 to hold values in vector char1. I am assuming the problem stems from the data type of v1 vs. char1, however, I have not been able to find a concrete solution. I do not want to read directly into a string vector; hence my current problem.
I am not sure what you try to achieve. Here few examples:
#include <string>
#include <vector>
int main()
{
std::string str1{ "Just for an example" }; // You can read it from a file
std::vector<std::string> vct_str1(32); // Lets say it has 32 std::string items
std::vector<std::string> vct_str2(32); // Lets say it has 32 std::string items
// **** A. Copy from std::string to std::vector<char>: ****
std::vector<char> vct_ch(str1.begin(), str1.end()); // On construction
// Or later: vct_ch.assign(str1.begin(), str1.end());
// **** B. Copy from std::vector<char> to std::string: ****
std::string str2(vct_ch.begin(), vct_ch.end()); // On construction
// Or later: str2.assign(vct_ch.begin(), vct_ch.end());
// **** C. Copy from std::vector<char> to std::vector<std::string>: ****
vct_str1[0].assign(vct_ch.begin(), vct_ch.end()); // Which is similar to B
// **** D. Source & Dest Types same as in Case-C But char per std::string: ****
int i = 0;
vct_str2.resize(vct_ch.size());
for (auto item : vct_ch)
vct_str2[i++] = item;
return 0;
}
I have a set of strings
set<string> strings;
How do I get the longest string contained in the set? In python I could do the following:
print max(strings, key=len)
Is there a similar function in c++?
You can use std::max_element that ships with the <algorithm> header and pass a custom comparison predicate.
#include <algorithm>
#include <iostream>
const auto longest = std::max_element(strings.cbegin(), strings.cend(),
[](const std::string& lhs, const std::string& rhs) { return lhs.size() < rhs.size(); });
if (longest != strings.cend())
std::cout << *longest << "\n";
This is clearly not as concise as the python version, and this is where ranges are to the rescue. With range-v3 projections, this boils down to
#include <range/v3/all.hpp>
const auto longest = ranges::max_element(strings, std::less<>{}, &std::string::size);
I'd like to build a rule that takes in a few parameters from a parsed line then sets a few as constant. Is that possible? An (invalid) example to illustrate what I'm trying to do is below. I think I'm using _r1 incorrectly here, but I'm not sure how to get at the right thing. Assume I don't want to just modify r before sending it into the parser.
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_plus.hpp>
#include <boost/spirit/include/qi_sequence.hpp>
#include <boost/spirit/include/qi_string.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/phoenix/bind/bind_function.hpp>
#include <string>
using namespace boost::spirit::qi;
struct Sample
{
int a;
int b;
};
BOOST_FUSION_ADAPT_STRUCT(Sample, a , b)
const rule<std::string::const_iterator, Sample()> AnythingAndOne = int_ >> eps[_r1.b = 1] >> eoi;
int main()
{
std::string z("3");
Sample r;
parse(z.begin(), z.end(), AnythingAndOne, r);
return 0;
}
Again, with reference to Boost Spirit: "Semantic actions are evil"? I'd avoid the semantic action.
You can directly synthesize a particular attribute value by using qi::attr:
Live On Coliru
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
struct Sample {
int a;
int b;
};
BOOST_FUSION_ADAPT_STRUCT(Sample, a , b)
namespace qi = boost::spirit::qi;
int main()
{
std::string const z("3");
Sample r;
qi::rule<std::string::const_iterator, Sample()> AnythingAndOne
= qi::int_ >> qi::attr(1) >> qi::eoi;
if (parse(z.begin(), z.end(), AnythingAndOne, r))
std::cout << "Parsed: " << boost::fusion::as_vector(r) << "\n";
else
std::cout << "Parse failed\n";
}
Prints
Parsed: (3 1)
I just started working with C++ about a week ago so I don't know much about the language. I am working on creating a program that inputs a text file and then reverses all of the lines.
So an input of:
"abc"
"123"
Would be:
"cba"
"321"
I can figure out how to input the lines from the file, but I am having trouble with my reverse function. I have tried to just print out each individual character as a string using substring, starting from length - 1 and ending at 0. But this does not seem to be working. When I run the program, I get these errors that I don't understand.. Any ideas as to what is going wrong?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void reverse(string input) {
for((int x=input.length()-1); x=0; x--)
cout << input.substr(x);
}
cout << endl;
}
int main()
{
string line;
ifstream myFile;
myFile.open("reverse_input.txt");
while(getline(myFile, line)) {
reverse(line);
}
return 0;
}
There are couple syntax errors in your code that the compiler was complaining
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void reverse(string input) {
for( unsigned int x= (input.length()-1); x>=0; x--) { //Missing {, extra (), >= insteadof = 0
cout << input[x] << flush;
}
cout << endl;
}
int main()
{
string line;
ifstream myFile;
myFile.open("reverse_input.txt");
while(getline(myFile, line)) {
reverse(line);
}
return 0;
}
Is it possible to count how many strings are equal to one given as a parameter using an algorithm method?
#include <algorithm>
#include <vector>
#include <string>
int main(){
vector<string> vectorPeople;
//assume that myVector isn't empty
string name;
cin >> name;
int total=std::count(myVector.begin(),myVector.end(),name);
}
Yes. You can use std::count (you could've found that easily by a simple search).
#include <vector>
#include <algorithm>
#include <string>
int count(std::vector<std::string> strings , std::string to_search){
return std::count(strings.begin() , strings.end() , to_search);
}