I have a long list of some good old fashioned c style strings:
const char * p1key = PROPERTY_MAX_THREADS;
const char * p1value = "12";
const char * p2key = PROPERTY_MAX_FRAMES;
const char * p2value = "400";
const char * p3key = PROPERTY_MAX_FRAMEMEMORY;
const char * p3value = "140";
...
Then I do some stuff with them:
// write p1, p2, p3, pn to disk in fancy format
At the end I want to be able to write a loop and compare the written values to the original values.
int numProperties = 20;
for (int i = 0; i < numProperties; ++i) {
// on the first iteration, access p1 key/value
// on the second, access p2 key/value
// ...
}
How can I access p1 on the first iteration, p2 on the second, etc? Would an array of pointers help? I'm struggling to come up with the syntax to make this work. Any help would be very much appreciated.
Edit:
I would consider the best answer to show both the C and C++ way
INTRODUCTION
You'd have to store the pointers in some sort of container to be able to iterate over them in the manner as you propose.
Since you are dealing with pairs, std::pair from <utility> seems like a perfect match. Wrapping these std::pairs in a container such as std::vector will make it very easy to iterate over them in a clean manner.
SAMPLE IMPLEMENTATION
#include <iostream>
#include <utility>
#include <vector>
#define PROPERTY_MAX_THREADS "max_threads"
#define PROPERTY_MAX_FRAMES "max_frames"
#define PROPERTY_MAX_FRAMEMEMORY "max_fmemory"
const char * p1key = PROPERTY_MAX_THREADS;
const char * p1value = "12";
const char * p2key = PROPERTY_MAX_FRAMES;
const char * p2value = "400";
const char * p3key = PROPERTY_MAX_FRAMEMEMORY;
const char * p3value = "140";
int
main (int argc, char *argv[])
{
std::vector<std::pair<char const *, char const *>> properties {
{ p1key, p1value }, { p2key, p2value }, { p3key, p3value }
};
std::cout << "properties:\n";
for (auto& it : properties) {
std::cout << " " << it.first << " = " << it.second << "\n";
}
}
properties:
max_threads = 12
max_frames = 400
max_fmemory = 140
I TRIED THE ABOVE BUT IT DOESN'T COMPILE, WHY?
The previously written snippet makes use of features introduced in C++11, if you are unable to compile such code you will need to resort to functionality that your compiler does provide.
Below is a modified implementation that can be compiled by any compiler that supports C++03:
int const PROPERTIES_LEN = 3;
std::pair<char const *, char const*> properties[PROPERTIES_LEN] = {
std::make_pair (p1key, p1value),
std::make_pair (p2key, p2value),
std::make_pair (p3key, p3value)
};
for (int i = 0; i < PROPERTIES_LEN; ++i) {
std::cout << properties[i].first << " = " << properties[i].second << "\n";
}
You tagged it C++, so I'm going to give the C++ suggestion.
#include <iostream>
#include <vector>
#include <utility>
#define PROPERTY_MAX_THREADS "1"
#define PROPERTY_MAX_FRAMES "2"
#define PROPERTY_MAX_FRAMEMEMORY "3"
const char * p1key = PROPERTY_MAX_THREADS;
const char * p1value = "12";
const char * p2key = PROPERTY_MAX_FRAMES;
const char * p2value = "400";
const char * p3key = PROPERTY_MAX_FRAMEMEMORY;
const char * p3value = "140";
int main() {
using namespace std;
vector<pair<const char*,const char *>> collection =
{{p1key,p1value},{p2key,p2value},{p3key,p3value}};
for(auto &ele : collection){
cout << "key:" << ele.first
<< "value:" << ele.second << endl;
}
return 0;
}
alternatively just declare it as a collection from the beginning
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#define PROPERTY_MAX_THREADS "1"
#define PROPERTY_MAX_FRAMES "2"
#define PROPERTY_MAX_FRAMEMEMORY "3"
int main() {
using namespace std;
vector<pair<const string,const string>> collection =
{
{PROPERTY_MAX_THREADS, "12" },
{PROPERTY_MAX_FRAMES, "400"},
{PROPERTY_MAX_FRAMEMEMORY, "140"}
};
for(auto &ele : collection){
cout << "key:" << ele.first
<< " value:" << ele.second << endl;
}
return 0;
}
In C you can do this way.
#define STRA_END 0
const char* keyArray[] = {
"string1",
"string2",
"string3",
STRA_END
}
const char* valueArray[] = {
"string1",
"string2",
"string3",
STRA_END
}
main(){
int i;
for( i=0; keyArray[i]!=0; ++i )
doSometingToString(keyArray[i], valueArray[i]);
}
Related
std::string mstring[5];
mstring[0] = "veena";
mstring[1] = "guitar";
mstring[2] = "sitar";
mstring[3] = "sarod";
mstring[4] = "mandolin";
I want to assign the array like above. I don't want to do it at initialization but assign later. Is there a way to combine 5 statements into one.
You can do that by using std::array<std::string, 5> instead of the raw array.
For example
#include <iostream>
#include <string>
#include <array>
int main()
{
std::array<std::string, 5> mstring;
mstring = { "veena", "guitar", "sitar", "sarod", "mandolin" };
for ( const auto &s : mstring )
{
std::cout << s << ' ';
}
std::cout << '\n';
}
The program output is
veena guitar sitar sarod mandolin
Another approach when a raw array is used is to use std::initializer_list in range-based for loop. For example
#include <iostream>
#include <string>
int main()
{
std::string mstring[5];
size_t i = 0;
for ( auto s : { "veena", "guitar", "sitar", "sarod", "mandolin" } )
{
mstring[i++] = s;
}
for ( const auto &s : mstring )
{
std::cout << s << ' ';
}
std::cout << '\n';
}
The program output is the same as shown above
veena guitar sitar sarod mandolin
If your compiler supports C++ 20 then instead of these statements
size_t i = 0;
for ( auto s : { "veena", "guitar", "sitar", "sarod", "mandolin" } )
{
mstring[i++] = s;
}
you can use just one range-based for loop
for ( size_t i = 0; auto s : { "veena", "guitar", "sitar", "sarod", "mandolin" } )
{
mstring[i++] = s;
}
This is a two-liner, but I think one-lining is not possible with native arrays (at least to my best knowledge).
Initialize a temporary array with the desired values
swap pointers with the original array
In that way you can use the one-line initialization for arrays, but still can manipulate the original array before setting the values.
#include <string>
#include <iostream>
int main(){
std::string mystring[5];
std::string tmp[5] = {"veena","guitar","sitar", "sarod", "mandolin"};
std::swap(mystring, tmp);
for(auto& s : mystring) std::cout << s << std::endl;
return 0;
}
You can use comma operator to combine multiple expressions into one statement.
mstring[0] = "veena",
mstring[1] = "guitar",
mstring[2] = "sitar",
mstring[3] = "sarod",
mstring[4] = "mandolin";
I have a method that creates a file header with a given comment symbol, depending on the output file type. There are only a few supported file types, thus I want to create these headers at compile time. Can I make these as a constexpr ?
std::string create_header(const std::string &comment_symbol) {
std::string div(15, '-');
std::string space(2, ' ');
std::stringstream hdr;
hdr << comment_symbol << space << " Begin of File." << std::endl;
hdr << comment_symbol << space << div << std::endl;
return hdr.str();
}
std::string c_header() { return create_header("//"); }
std::string python_header() { return create_header("#");
Can I create constexpr strings with functions?
You can't return std::string, cause it allocates memory.
Can I make these as a constexpr ?
Sure, something along:
#include <array>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
template<size_t N> constexpr
auto create_header(const char (&comment_symbol)[N]) {
const char one[] = " Begin of File.\n";
const char two[] = " -\n";
std::array<char,
N + (sizeof(one) - 1) +
N + (sizeof(two) - 1) + 1
> ret{};
auto it = ret.begin();
for (const char *i = comment_symbol; *i; ++i) *it++ = *i;
for (const char *i = one; *i; ++i) *it++ = *i;
for (const char *i = comment_symbol; *i; ++i) *it++ = *i;
for (const char *i = two; *i; ++i) *it++ = *i;
return ret;
}
std::string c_header() {
constexpr auto a = create_header("//");
return std::string{a.begin(), a.end()};
}
int main() {
std::cout << c_header() << '\n';
}
Is there a better way to search for the index of the first element using a predicate?
// ... this code looks a bit long to me. Anyway to do better?
auto it = std::find_if(collection + startAt,
collection + COLLECTION_SIZE,
[](const char* line) { return strlen(line) <= 10; });
int idx = std::dist(collection, it); //= it - collection;
This is my attempt to refactor the C-style code below:
for (posEmptyItem = startAt; strlen(collection[posEmptyItem]) > 10; posEmptyItem++) {}
std::cout << posEmptyItem << std::endl;
Here a complete example of
#include "stdafx.h"
#include <cstring>
#include <iostream>
#include <algorithm>
#define COLLECTION_SIZE 123
int main()
{
char* collection[COLLECTION_SIZE]{ "time11,time2,time3",
"time12,time2,time3",
"time13,time2,time3",
"time14,time2,time3",
"time15,time2,time3",
"x\n",
"" };
auto startAt = 2;
int posEmptyItem;
// legacy code
for (posEmptyItem = startAt; strlen(collection[posEmptyItem]) > 10; posEmptyItem++) {}
std::cout << posEmptyItem << std::endl;
// replace the loop to search an index by calling to standard library
auto it = std::find_if(collection + startAt,
collection + COLLECTION_SIZE,
[](const char* line) { return strlen(line) <= 10; });
posEmptyItem = it - collection;
std::cout << posEmptyItem << std::endl;
return 0;
}
I have a vector of type struct with some elements, and trying to count the number of occurrences of an element(value) in its corresponding column of the vector. I know how to count on a simple vector, e.g on vector of type string. But am stuck on vector<struct>. Any possible solution or suggestion?
Sample code:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
struct my_struct
{
std::string first_name;
std::string last_name;
};
int main()
{
std::vector<my_struct> my_vector(5);
my_vector[0].first_name = "David";
my_vector[0].last_name = "Andriw";
my_vector[1].first_name = "Jhon";
my_vector[1].last_name = "Monta";
my_vector[2].first_name = "Jams";
my_vector[2].last_name = "Ruth";
my_vector[3].first_name = "David";
my_vector[3].last_name = "AAA";
my_vector[4].first_name = "Jhon";
my_vector[4].last_name = "BBB";
for(int i = 0; i < my_vector.size(); i++)
{
int my_count=count(my_vector.begin(), my_vector.end(),my_vector[i].first_name);
/*I need help to count the number of occerencess of each "First_name" in a vector
For example: First_Name:- David COUNT:- 2 ...and so on for each first_names*/
std::cout << "First_Name: " << my_vector[i].first_name << "\tCOUNT: " << my_count << std::endl;
}
return 0;
}
but, the same code for a vector of type string,std::vector<std::string> works properly. see below:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> my_vector;
my_vector.push_back("David");
my_vector.push_back("Jhon");
my_vector.push_back("Jams");
my_vector.push_back("David");
my_vector.push_back("Jhon");
for(int i = 0; i < my_vector.size(); i++)
{
int my_count = count(my_vector.begin(), my_vector.end(),my_vector[i]); //this works good
std::cout << "First_Name: " << my_vector[i] << "\tCOUNT: " << my_count << std::endl;
}
return 0;
}
You have to use std::count_if with correct predicate:
int my_count = std::count_if(my_vector.begin(), my_vector.end(),
[&](const my_struct& s) {
return s.first_name == my_vector[i].first_name;
});
Demo
The functor to replace lambda in C++03:
struct CompareFirstName
{
explicit CompareFirstName(const std::string& s) : first_name(s) {}
bool operator () (const my_struct& person) const
{
return person.first_name == first_name;
}
std::string first_name;
};
and then
int my_count = std::count_if(my_vector.begin(), my_vector.end(),
CompareFirstName(my_vector[i].first_name));
Demo
What do I have to do so that when I
string s = ".";
If I do
cout << s * 2;
Will it be the same as
cout << "..";
?
std::string has a constructor of the form
std::string(size_type count, char c);
that will repeat the character. For example
#include <iostream>
int main() {
std::string stuff(2, '.');
std::cout << stuff << std::endl;
return 0;
}
will output
..
I used operator overloading to simulate this behavior in c++.
#include <iostream>
#include <string>
using namespace std;
/* Overloading * operator */
string operator * (string a, unsigned int b) {
string output = "";
while (b--) {
output += a;
}
return output;
}
int main() {
string str = "abc";
cout << (str * 2);
return 0;
}
Output:
abcabc
No, std::string has no operator *. You can add (char, string) to other string. Look at this http://en.cppreference.com/w/cpp/string/basic_string
And if you want this behaviour (no advice this) you can use something like this
#include <iostream>
#include <string>
template<typename Char, typename Traits, typename Allocator>
std::basic_string<Char, Traits, Allocator> operator *
(const std::basic_string<Char, Traits, Allocator> s, size_t n)
{
std::basic_string<Char, Traits, Allocator> tmp = s;
for (size_t i = 0; i < n; ++i)
{
tmp += s;
}
return tmp;
}
template<typename Char, typename Traits, typename Allocator>
std::basic_string<Char, Traits, Allocator> operator *
(size_t n, const std::basic_string<Char, Traits, Allocator>& s)
{
return s * n;
}
int main()
{
std::string s = "a";
std::cout << s * 5 << std::endl;
std::cout << 5 * s << std::endl;
std::wstring ws = L"a";
std::wcout << ws * 5 << std::endl;
std::wcout << 5 * ws << std::endl;
}
http://liveworkspace.org/code/52f7877b88cd0fba4622fab885907313
There is no predefined * operator that will multiply a string by an int, but you can define your own:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string operator*(const string& s, unsigned int n) {
stringstream out;
while (n--)
out << s;
return out.str();
}
string operator*(unsigned int n, const string& s) { return s * n; }
int main(int, char **) {
string s = ".";
cout << s * 3 << endl;
cout << 3 * s << endl;
}
They can't be multipled but I think you can write your own function to do this, something like -
#include <iostream>
#include <string>
std::string operator*(std::string s, size_t count)
{
std::string ret;
for(size_t i = 0; i < count; ++i)
{
ret = ret + s;
}
return ret;
}
int main()
{
std::string data = "+";
std::cout << data * 10 << "\n";
}
It's probably not the best idea though, it will be very confusing to anyone looking at the code and not expecting this,
Strings cannot be multiplied.
If s is a char
'.' // This has ASCII code 46
then
cout << (char)((int)s * 2);
will give you
'/' // This has ASCII code 92
Like JRG did, but in a single line
std::cout << std::string(70,'-') << std::endl;
This will create a string, filled with - (dashes), 70 characters long, and breaking the line at the end with std::endl;
You can do this:
#include <iostream>
using namespace std;
int main()
{
string text, new_text;
int multiply_number;
cin >> text >> multiply_number;
/*
First time in the 'for' loop: new_text = new_text + text
new_text = "" + "your text"
new_text = "your text"
Second time in the 'for' loop: new_text = new_text + text
new_text = "your text" + "your text"
new_text = "your textyour text"...n times
*/
for(int i=0; i<multiply_number; i++)
{
new_text += text;
}
cout << new_text << endl; // endl="\n"
system("pause");
return 0;
}
In Python you can multiply string like this:
text = "(Your text)"
print(text*200)
std::string StrMultiply(const char* str, size_t count) {
size_t stringsize = strlen(str);
size_t buffersize = stringsize * count + 1;
string res(buffersize,'\0');
char* end = res._Unchecked_end();
char* offset = res._Unchecked_begin();
for (size_t i = 0;i < count; offset += stringsize,i++)
{
memcpy(offset, str, stringsize);
}
// mark the end
res[buffersize - 1] = '\0';
return res;
}
inline std::string operator*(std::string left, size_t right) {
return StrMultiply(left.c_str(), right);
}
here is a ram-friendly solution, 10 times faster than using stringstreams or string::append
It's surprising that nobody talked about this yet. For assigning char*int to a variable, you can do str.assign(n, char). https://en.cppreference.com/w/cpp/string/basic_string/assign
For example, str.assign(2, 's') would yield ss for the value of str. Other methods to actually achieve the objective have already been mentioned.